手势识别器

UITapGestureRecognizer是轻拍⼿手势识别器,能识别轻拍操作

UILongPressGestureRecognizer是⻓长按⼿手势识别器,能识别⻓长按操作。

UIRotationGestureRecognizer是旋转⼿手势识别器,能识别旋转操作。

UIPinchGestureRecognizer是捏合⼿手势识别器,能识别捏合操作。

UIPanGestureRecognizer是平移⼿手势识别器,能识别拖拽操作。

UISwipeGestureRecognizer是轻扫⼿手势识别器,能识别拖拽操作。

UIScreenEdgePanGestureRecognizer是屏幕边缘轻扫识别器,是iOS7中新增的⼿手势。 

以轻拍、旋转、捏合为例

#import "RootViewController.h"

@interface RootViewController () <UIGestureRecognizerDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建一个轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doTap:)];
    [imageView addGestureRecognizer:tap];
    [tap release];
    
    // 创建一个旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(doRotation:)];
    rotation.delegate = self;
    [imageView addGestureRecognizer:rotation];
    [rotation release];

    // 创建一个捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)];
    pinch.delegate = self;
    [imageView addGestureRecognizer:pinch];
    [pinch release];
}    

- (void)doTap:(UITapGestureRecognizer *)tap
{
    // 通过tap.view可以获取当前手势放置在哪一个view上
    NSLog(@"被点击了,(⊙o⊙)哦,NO %@", tap.view);
}

- (void)doRotation:(UIRotationGestureRecognizer *)rotationGesture
{
    NSLog(@"转啦,啊哈哈");
    // 在做旋转的时候对应的有两个函数,一个是CGAffineTransformRotate,还有一个是CGAffineTransformMakeRotation。这两个函数的区别就在于第一个函数可以在上次的基础上继续旋转,第二个函数只能做一次旋转
    // 1.在上次的基础上做旋转
    rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
    // 2.将手势识别器的旋转角度归零,下次让其在新的旋转角度上进行旋转,而不是累加。
    rotationGesture.rotation = 0;
}

- (void)doPinch:(UIPinchGestureRecognizer *)pinchGesture
{
    pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
    // 设置比例为1,可以让大小的缩放和手指缩放同步
    pinchGesture.scale = 1;
}

// 想要在旋转的过程中也能捏合,需要实现代理中得这个方法,返回YES就可以
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

  

原文地址:https://www.cnblogs.com/sqdhy-zq/p/4764433.html