UIPinchGestureRecognizer捏合手势

概述:UIPinchGestureRecognizer捏合手势,继承于UIGestureRecognizer类,相对于UIGestureRecognizer多了两个属性值。

 /*获取缩放(捏合)的大小*/
@property (nonatomic)          CGFloat scale; 
 /*获取缩放(捏合)的速度*/
@property (nonatomic,readonly) CGFloat velocity;  

案例:图片的放大缩小

 UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)];
 [pinchGestureRecognizer setDelegate:self];
/*加载到要缩放的图片*/
 [self.image_view addGestureRecognizer:pinchGestureRecognizer];
- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer{
     /*获取状态*/
    UIGestureRecognizerState state = [recognizer state];
    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged){
       /*获取捏合大小比例*/
       CGFloat scale = [recognizer scale];
       /*获取捏合的速度*/
       CGFloat velocity = [recognizer velocity];
       NSLog(@"velocity %f",velocity);
       [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
       [recognizer setScale:1.0];
       if (self.image_view.frame.size.width <= self.view.bounds.size.width) {
            self.image_view.frame = self.view.bounds;
       }
    }
}

注:了解UIGestureRecognizer类请跳转https://www.jianshu.com/p/e206dc86f89a

你可能感兴趣的:(UIPinchGestureRecognizer捏合手势)