iOS开发五种触屏事件的处理

UIGestureRecognizer:是一个抽象类,表示触屏手势,这个类没有具体的作用。实际中经常使用的是这个类的子类:

UITapGestureRecognizer(敲击手势),UILongPressGestureRecognizer(长按手势),UISwipeGestureRecognizer(清扫手势),UIPinchGestureRecognizer(捏合手势),UIPanGestureRecognizer(拖拽手势)。这五种手势的使用方法大同小异,下面介绍具体的使用方法:

1. UITapGestureRecognizer(敲击手势)

    UITapGestureRecognizer *tapgest = [[UITapGestureRecognizer alloc] init];

    //属性 每个手势事件所带有的属性不同

    //敲击事件有两个重要属性:一个是敲击的次数,一个是敲击的手指数,分别代表这个敲击事件需要敲击几次屏幕和几个手指敲击可以响应

    tapgest.numberOfTapsRequired = 2;

    tapgest.numberOfTouchesRequired = 1;

     tapgest.delegate = self;

    //监听事件,监听的这个事件一般带一个参数,这个参数就是这个手势本身

    [tapgest addTarget:self action:@selector(tapgestView:)];

    //添加到view

    [self.imageView addGestureRecognizer:tapgest];

2. UILongPressGestureRecognizer(长按手势)

    //长按  手势有开始的状态和结束的状态,而  长按时间 属性表示开始状态之前的时间,即按多长时间响应手势,松开手指时候的状态是结束状态 (开始状态和结束状态之间没有时间限制)

 //手势2 长按手势

 UILongPressGestureRecognizer *longgesture = [[UILongPressGestureRecognizer alloc] init];

    //属性

    //长按时间

    longgesture.minimumPressDuration = 3;

    //移动距离,在这个距离范围内按手势有用

    longgesture.allowableMovement = 30;

    

    //添加事件

    [longgesture addTarget:self action:@selector(longpressView:)];

    //添加到view

    [self.imageView addGestureRecognizer:longgesture];

3. UISwipeGestureRecognizer(清扫手势)

//3.清扫手势

    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipView:)];

    //属性

    //轻扫手势有一个轻扫的方向,默认的方向是向右,可以改变

    swip.direction = UISwipeGestureRecognizerDirectionUp;

    //添加到view

    [self.imageView addGestureRecognizer:swip];

4.  UIPinchGestureRecognizer(捏合手势)

//4.捏合手势

    UIPinchGestureRecognizer *pinchgesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];

    //添加到view

    [self.imageView addGestureRecognizer:pinchgesture];

5. UIPanGestureRecognizer(拖拽手势)

//5.拖拽手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];

    [self.imageView addGestureRecognizer:pan];

原文地址:https://www.cnblogs.com/xiaofei993/p/5342809.html