标签:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureRecognizerStatePossible, // 一个手势已经开始但尚未改变或者完成时 UIGestureRecognizerStateBegan, // 手势状态改变 UIGestureRecognizerStateChanged, // 手势完成 UIGestureRecognizerStateEnded, // 手势取消,恢复至Possible状态 UIGestureRecognizerStateCancelled, // 手势失败,恢复至Possible状态 UIGestureRecognizerStateFailed, // 识别到手势识别 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded };
target
action:(SEL)action
target
action:(SEL)action
target
action:(SEL)action
view
locationInView:时,(1)
如果是多次点击,location是第一次点击是的位置;#pragma mark - 点按手势 - (void)tap:(UITapGestureRecognizer *)recognizer { // 双击放大图片 UIImageView *imageView = (UIImageView *)recognizer.view; [UIView animateWithDuration:0.8f animations:^{ [imageView setTransform:CGAffineTransformScale(imageView.transform, 2.0, 2.0)]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.5f animations:^{ [imageView setTransform:CGAffineTransformScale(imageView.transform, 0.5, 0.5)]; }]; }]; }
#pragma mark - 捏合 - (void)pinch:(UIPinchGestureRecognizer *)recognizer { // 调整图像的显示比例 if (UIGestureRecognizerStateChanged == recognizer.state) { // 等比例缩放 [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale)]; // 在设置累加形变参数时,需要重置手势的缩放比例 recognizer.scale = 1.0f; } }
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) { UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << 3 };
#pragma mark - 轻扫 - (void)swipe:(UISwipeGestureRecognizer *)recognizer { NSLog(@"%d", recognizer.direction); if (UISwipeGestureRecognizerDirectionDown == recognizer.direction) { NSLog(@"向下"); } else if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) { NSLog(@"向上"); } else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) { NSLog(@"向左"); } else if (UISwipeGestureRecognizerDirectionRight == recognizer.direction) { NSLog(@"向右"); } }
#pragma mark - 拖动 - (void)pan:(UIPanGestureRecognizer *)recognizer { CGPoint translation = [recognizer translationInView:self.view]; // 让图像根据手指移动 if (UIGestureRecognizerStateChanged == recognizer.state) { // 形变参数,有Make的时候,是相对初始位置计算的 // 没有Make是递增修改形变的 [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)]; // 拖动手指中的平移距离是相对于初始位置,如果使用CGAffineTransformTranslate累加形变方法 // 需要在每次位移之后,重置recognizer的位移量,就是将位移量清零 [recognizer setTranslation:CGPointZero inView:recognizer.view]; } }
#pragma mark - 旋转 - (void)rotate:(UIRotationGestureRecognizer *)recognizer { if (UIGestureRecognizerStateChanged == recognizer.state) { recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); // 重置旋转量 recognizer.rotation = 0.0f; } }
#pragma mark - 长按手势 - (void)longPress:(UILongPressGestureRecognizer *)recognizer { NSLog(@"长按"); // 长按旋转图片 // 开始长按 // 在形变时,iOS采取就近原则旋转,如果不能按照希望的方向旋转,可以增加一些修正值,例如0.01 // 直接通过形变属性,要实现一次性转一圈,比较困难,可以分两次进行 if (UIGestureRecognizerStateBegan == recognizer.state) { [UIView animateWithDuration:0.8f animations:^{ [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)]; }]; } else if (UIGestureRecognizerStateEnded == recognizer.state) { [UIView animateWithDuration:0.8f animations:^{ [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)]; } completion:^(BOOL finished) { // 清除所有的形变属性 [recognizer.view setTransform:CGAffineTransformIdentity]; }]; } }
标签:
原文地址:http://www.cnblogs.com/kangshang/p/4420913.html