20.iOS手势识别

轻击手势(TapGestureRecognizer)
轻扫手势 (SwipeGestureRecognizer)
长按手势(LongPressGestureRecognizer)
拖动手势(PanGestureRecognizer)
捏合手势(PinchGestureRecognizer)
旋转手势(RotationGestureRecognizer)

@interface ViewController () 


@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 1.添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    // tap.numberOfTapsRequired = 2; // 要求双击才触发事件
    // tap.numberOfTouchesRequired = 2; // 要求两个手指同时触摸才触发事件
    
    tap.delegate = self;
    // 将点击手势添加到imageView上
    [_imageView addGestureRecognizer:tap];
    
    // 2.添加移动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [_imageView addGestureRecognizer:pan];
    pan.delegate = self;
    
    // 3.添加缩放手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [_imageView addGestureRecognizer:pinch];
    pinch.delegate = self;
    
    // 4.旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [_imageView addGestureRecognizer:rotation];
    rotation.delegate = self;
    
    // 5.轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    // 设置轻扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [_imageView addGestureRecognizer:swipe];
    swipe.delegate = self;
    
    // 添加长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    //设置长按时间
    longPress.minimumPressDuration = 0.5;
    [_imageView addGestureRecognizer:longPress];
    longPress.delegate = self;
    
}

- (void)tap:(UITapGestureRecognizer *)tap {
    NSLog(@"tap---");
}

- (void)pan:(UIPanGestureRecognizer *)pan {
    // 获得移动手势在self.view上面的偏移量(x, y)
    CGPoint point = [pan translationInView:self.view];
    // 让图片视图随着手指移动
    pan.view.center = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);
    // 重置手势获取的偏移量
    [pan setTranslation:CGPointZero inView:self.view];
}

- (void)pinch:(UIPinchGestureRecognizer *)pinch {
    // 参数一:原来的transform
    // 参数二:水平方向缩放的倍数
    // 参数三:垂直方向缩放的倍数
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 重置缩放倍数
    pinch.scale = 1.0;
}

- (void)rotation:(UIRotationGestureRecognizer *)rotation {
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    // 重置角度
    rotation.rotation = 0;
}

- (void)swipe:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"swipe");
}

- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    /*
     说明:长按手势的常用状态如下
     开始:UIGestureRecognizerStateBegan
     改变:UIGestureRecognizerStateChanged
     结束:UIGestureRecognizerStateEnded
     取消:UIGestureRecognizerStateCancelled
     失败:UIGestureRecognizerStateFailed
     */
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"longPress");
    }
}


// 设置同时可以有两个手势同时识别
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

你可能感兴趣的:(20.iOS手势识别)