最新消息:文章中包含代码时,请遵守代码高亮规范!

iOS – 几种常见手势的简单使用[原创]

iOS 石, 稳 318浏览 0评论

1、UIGestureRecognizer 介绍

手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性。

iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。

  • UIPanGestureRecognizer(拖动)
  • UIPinchGestureRecognizer(捏合)
  • UIRotationGestureRecognizer(旋转)
  • UITapGestureRecognizer(点按)
  • UILongPressGestureRecognizer(长按)
  • ​UISwipeGestureRecognizer(轻扫)

另外,可以通过继承 UIGestureRecognizer 类,实现自定义手势(手势识别器类)。

使用手势很简单,分为三步:

  1. 创建手势识别器对象实例。创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。
  2. 设置手势识别器对象实例的相关属性(可选部分)
  3. 添加到需要识别的 View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。

值得注意的地方:

一个手势只能对应一个 View,但是一个 View 可以有多个手势。建议在真机上测试这些手势,模拟器操作不太方便,可能导致认为手势失效的情况。(

  • 平移(Pan)、旋转(Rotation)、捏合(Pinch)事件都是需要进行清零操作。
  • 捏合手势不能和旋转手势一起使用。

UIGestureRecognizer 的继承关系如下:

2、具体的使用步骤

2.1.点按手势:UITapGestureRecognizer

首先要声明UIGestureRecognizerDelegate代理的方法,继承代理。

添加点按手势:

//创建点按手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(tap)];
    //继承代理
    tap.delegate = self ;
    //添加手势
    [self.imagView addGestureRecognizer:tap] ;

继承代理:值得注意的是只有点按手势有代理方法

//继承代理
tap.delegate = self ;

实现点按手势的代理方法:

//是否允许接受手指
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    //判断点在图片的左边还是右边
    //在左边返回YES,反之在NO
    //1.获取当前的点
    CGPoint curP = [touch locationInView:self.imagView] ;
    if (curP.x > self.imagView.bounds.size.width * 0.5) {
        return YES ;
    }else{
        return NO ;
    }
}

其他的代理方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer ;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer ;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press

2.2.长按手势:UILongPressGestureRecognizer

添加长按手势:

UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)] ;
    [self.view addGestureRecognizer:longP] ;

实现长按手势的方法:

-(void)longPress:(UILongPressGestureRecognizer*)longP{    
//通过UILongPressGestureRecognizer的state属性来完成细节操作   
 /*     UIGestureRecognizerStateBegan,     UIGestureRecognizerStateChanged,     UIGestureRecognizerStateEnded,     UIGestureRecognizerStateCancelled,          UIGestureRecognizerStateFailed,     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded     */       
 if(longP.state== UIGestureRecognizerStateBegan) {        
NSLog(@"UIGestureRecognizerStateBegan") ;    }
elseif(longP.state== UIGestureRecognizerStateChanged){        
NSLog(@"UIGestureRecognizerStateChanged") ;    }elseif(longP.state== UIGestureRecognizerStateEnded){        
NSLog(@"UIGestureRecognizerStateEnded") ;    
}    
NSLog(@"%s",__func__) ;}

UILongPressGestureRecognizer的不同状态下的枚举类型:

1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
2     UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
3     UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
4     UIGestureRecognizerStateChanged,    // 手势状态发生转变
5     UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)
6     UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态
7     UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态
8     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
9 };

2.3.轻扫手势:UISwipeGestureRecognizer

添加轻扫手势:

//添加轻扫手势    UISwipeGestureRecognizer*swipeGes = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)] ;

注意点:轻扫手势默认是允许向右轻扫,可以通过swipeGes.direction属性来更改

//默认是向右轻扫
    //可以设置轻扫的方向(注意:只能左对右 或者上对下 不能左对上下)
    /*
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
     */
    //&#x26a0;&#xfe0f;:一个轻扫手势只能对应一个方向,上面设置了多个方向,调用的方法也只能是一个结果。
   swipeGes.direction = UISwipeGestureRecognizerDirectionRight ;
   [self.imagView addGestureRecognizer:swipeGes] ;

添加手势
UISwipeGestureRecognizer *swipeGes1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)] ;

实现方法:

-(void)swipe:(UISwipeGestureRecognizer *)swipeGes{
    
    if (swipeGes.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"UISwipeGestureRecognizerDirectionLeft") ;
    }else if (swipeGes.direction == UISwipeGestureRecognizerDirectionRight){
        NSLog(@"UISwipeGestureRecognizerDirectionRight") ;
    }
    
    if (swipeGes.state == UIGestureRecognizerStateBegan){
        NSLog(@"swipeGes.state == UIGestureRecognizerStateBegan") ;
    }else if (swipeGes.state == UIGestureRecognizerStateEnded){
        NSLog(@"swipeGes.state == UIGestureRecognizerStateEnded") ;
    }
//    NSLog(@"%s",__func__) ;
    
}

2.4.平移手势:UIPanGestureRecognizer

添加平移手势:

//添加平移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)] ;
    [self.imagView addGestureRecognizer:pan] ;

实现方法:值得注意的是:要进行清零操作 : [panGes setTranslation:CGPointMake(0, 0)]

-(void)pan:(UIPanGestureRecognizer *)panGes{
    CGPoint point = [panGes translationInView:self.imagView] ;
    NSLog(@"%@",NSStringFromCGPoint(point)) ;
    
    //相对于原点(错误做法)
//    self.imagView.transform = CGAffineTransformMakeTranslation(point.x, point.y) ;
    //相对于每次移动的点
    self.imagView.transform = CGAffineTransformTranslate(self.imagView.transform, point.x, point.y) ;
    
    //关键操作:清零操作
//    CGPointMake(0, 0) == CGPointZero
    [panGes setTranslation:CGPointMake(0, 0) inView:self.imagView] ;
}

2.5.旋转手势:UIRotationGestureRecognizer

添加旋转手势:

-(void)rota{
    //添加旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)] ;
    [self.view addGestureRecognizer:rotation] ;
}
-(void)rotation:(UIRotationGestureRecognizer *)rotationGes{
    //获取旋转的角度(已经是弧度了)
    //相对于最原始的y弧度
    CGFloat rotation = rotationGes.rotation ;
    self.imagView.transform = CGAffineTransformRotate(self.imagView.transform, rotation) ;
    //重要部分 : 清零操作
    [rotationGes setRotation:0] ;
}

2.6.捏合手势:UIPinchGestureRecognizer

#pragma mark - 创建捏合手势
-(void)pinch{
    //创建捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)] ;
    
    [self.imagView addGestureRecognizer:pinch] ;
}
-(void)pinch:(UIPinchGestureRecognizer *)pinchGes{
    //获取放大和缩放比例
    //相对于原始的比例而言
    CGFloat scale = pinchGes.scale ;
    self.imagView.transform = CGAffineTransformScale(self.imagView.transform, scale, scale) ;
    //清零操作
    [pinchGes setScale:1] ;
}

返回YES表示同时支持识别多个手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
returnYES; 
}

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/25012.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Annwn的打赏,我们会更加努力!    如果您想成为作者,请点我

发表我的评论
取消评论

表情