iOS开发之手势识别

作者: 李小南 | 来源:发表于2016-03-05 23:10 被阅读809次

如果这篇文章帮助到了您,希望您能点击一下喜欢或者关注我,有什么想法大家也可用一起交流,进步!

手势识别

  • 如果想监听一个view上面的触摸事件,上一篇文章的做法是自定义一个view,实现view的touches方法,在方法内部实现具体处理代码
  • 通过touches方法监听view触摸事件,有很明显的几个缺点
    1.必须得自定义view,在自定义的View当中去实现touches方法.
    2.由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
    3.不容易区分用户的具体手势行为(不容易区分是长按手势,还是缩放手势)这些等.
  • iOS3.2之后,苹果推出了手势识别功能(GestureRecognizer),在触摸事件处理方面,大大简化了开发者的开发难度

UIGestureRecognizer手势识别器

  • 利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
  • UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
  • UIGestureRecognizer的子类有:
    UITapGestureRecognizer(敲击)
    UIPinchGestureRecognizer(捏合,用于缩放)
    UIPanGestureRecognizer(拖拽)
    UISwipeGestureRecognizer(轻扫)
    UIRotationGestureRecognizer(旋转)
    UILongPressGestureRecognizer(长按)

手势使用的方法

  • 手势使用的方法大同小异
    1.创建手势
    2.添加手势
    3.实现手势方法

UITapGestureRecognizer(点按手势)

实现一张图片的左边可用点击,右边不能点击

添加点按手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]; 
手势也可以设置代理
tap.delegate = self; 
添加手势
[self.imageV addGestureRecognizer:tap];

代理方法:
是否允许接收手指
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    让图片的左边不可以点击, 
    获取当前手指所在的点.是在图片的左边还是在图片的右边. 
    CGPoint curP = [touch locationInView:self.imageV];
    if (curP.x > self.imageV.bounds.size.width * 0.5) {
        在图片的右侧
        return YES;
    }else{
        在图片的左侧
        return NO;
    }
    return YES;
}

UILongPressGestureRecognizer(长按)

添加长按手势
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];  
[self.imageV addGestureRecognizer:longP];

当长按时调用.
这个方法会调用很多次, 当手指长按在上面不松,来回移动时,会持续调用. 
所以要判断它的状态.
- (void)longP:(UILongPressGestureRecognizer *)longP{
    if(longP.state == UIGestureRecognizerStateBegan){
        NSLog(@"开始长按");
    }else if(longP.state == UIGestureRecognizerStateChanged){
        NSLog(@"长按时手指移动");
    }else if(longP.state == UIGestureRecognizerStateEnded){
        NSLog(@"手指离开屏幕");
}

UISwipeGestureRecognizer(轻扫手势)

添加轻扫手势
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
轻扫手势默认是向右边称轻扫
可以设置轻扫的方向.
一个轻扫手势只能设置一个方向的轻扫.想要让它有多个方向的手势,必须得要设置的多个轻扫手势
  swipe2.direction =  UISwipeGestureRecognizerDirectionUp;
  [self.imageV addGestureRecognizer:swipe2];


- (void)swipe:(UISwipeGestureRecognizer *)swipe{
     判断的轻扫的方向
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左轻扫");
    }else if(swipe.direction == UISwipeGestureRecognizerDirectionUp){
        NSLog(@"向上轻扫");
    }
}

UIPanGestureRecognizer(拖拽手势)

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

当手指拖动时调用
- (void)pan:(UIPanGestureRecognizer *)pan{
   
    拖动手势也有状态 
    if(pan.state == UIGestureRecognizerStateBegan){
          开始拖动  
    }else if(pan.state == UIGestureRecognizerStateChanged){
        注意:获取偏移量是相对于最原始的点 
        CGPoint transP = [pan translationInView:self.imageV];
        self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);

        复位,让它相对于上一次. 
        [pan setTranslation:CGPointZero inView:self.imageV];
    }else if(pan.state == UIGestureRecognizerStateEnded){
        结束拖动       
    }
}

UIPinchGestureRecognizer(捏合手势,用于缩放)与UIRotationGestureRecognizer(旋转手势)

添加捏合手势
UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGes:)];
设置代理使其能够同时支持多个手势(捏合的时候同时支持旋转)
pinGes.delegate = self;
[self.imageV addGestureRecognizer:pinGes];


旋转时调用
- (void)pinGes:(UIPinchGestureRecognizer *)pin{
    self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pin.scale, pin.scale);
    复位 
    [pin setScale:1];
   
}

添加旋转手势
 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
 设置代理使其能够同时支持多个手势(捏合的时候同时支持旋转)
 rotation.delegate = self;
 [self.imageV addGestureRecognizer:rotation];


当手指开始旋转时调用.
- (void)rotation:(UIRotationGestureRecognizer *)rotation{ 
    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation);
    复位. 
    [rotation setRotation:0]; 
}

是否允许支持多个手势----代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

相关文章

网友评论

    本文标题:iOS开发之手势识别

    本文链接:https://www.haomeiwen.com/subject/jacakttx.html