手势识别

作者: 大玲_ | 来源:发表于2015-04-16 20:38 被阅读449次

手势识别

6种手势识别

在iOS开发中有6中手势识别:
点按、捏合、拖动、轻扫、旋转、长按
苹果推出手势识别主要是为了:统一用户体验和简化开发难度。

UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer

手势识别的一般步骤:

  1. 实例化手势
  2. 指定手势参数
  3. 将手势附加到指定视图
  4. 编写手势监听方法

点按手势

// 实例化手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    //设置参数
    //点按次数:例如单击2次。
    [tap setNumberOfTapsRequired:2];
    //用几根手指点按
    [tap setNumberOfTouchesRequired:1];
    //添加到指定视图
    [imageView addGestureRecognizer:tap];
   

上面做的是前三步骤,下面是 添加监听方法

#pragma mark 点按手势
- (void)tap:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"点按我了");
    //recognizer.view就是识别的视图,也就是绑定到的视图。
}

*此处要注意recognizer.view的使用

拖动手势

具体看代码:

//2.长按手势
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
    [imageView addGestureRecognizer:longTap];
    

具体有什么参数 可以追到UILongPressGestureRecognizer类总查看。

监听方法与点击tap一样不在赘述。

拖动 旋转 捏合

    //3.拖动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [imageView addGestureRecognizer:pan];
    //4.旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [imageView addGestureRecognizer:rotation];
    //5.缩放、捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [imageView addGestureRecognizer:pinch];

注意:旋转有属性: recognizer.rotation 旋转的角度。
捏合有属性: recognizer.scale 捏和的大小
拖动 : translationInView 判断在父视图中的平移位置,平移位置以视图的初始位置为基础。

清扫 最复杂的手势 有四个方向

    UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    [swipe1 setDirection:UISwipeGestureRecognizerDirectionUp];
    [self.view addGestureRecognizer:swipe1];
    
    UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    [swipe2 setDirection:UISwipeGestureRecognizerDirectionDown];
    [self.view addGestureRecognizer:swipe2];
    
    UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    [swipe3 setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:swipe3];
    
    UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    [swipe4 setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.view addGestureRecognizer:swipe4];



注意:
1.要设置四个方向
2.要添加到俯视图
3.监听方法里不要使用。recognizer.view
4.手指离开屏幕触发

  • 清扫手势的监听事件 ,要判断方向
      CGRect frame = kImageFrame;
     NSLog(@"清扫手势 =====");
    if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) {
      frame.origin.y -=400;
     }else if (UISwipeGestureRecognizerDirectionDown == recognizer.direction){
      frame.origin.y +=400;
     }else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction){
    

frame.origin.x -=300;
}else{
frame.origin.x +=300;
}
[UIView animateWithDuration:2.0f animations:^{
[self.imageView setFrame:frame];
} completion:^(BOOL finished) {
[self.imageView setFrame:kImageFrame];
}];

** 要在监听方法中判断清扫的方向。
UIGestureRecognizer是一个抽象类 , 用于自定义手势 ,不能实例化。


相关文章

  • 手势控制:点击、滑动、平移、捏合、旋转、长按、轻扫

    手势识别器(Gesture Recognizer)用于识别触摸序列并触发响应事件。当手势识别器识别到一个手势或手势...

  • 3.6 iOS手势识别的状态和手势识别器幕后原理

    2.2手势识别的状态和手势识别器幕后原理 (一)手势的状态 (二)离散型手势识别器和连续型手势识别器之间的对比: ...

  • Gesture手势

    手势识别器 手势识别器是对触摸事件做了封装,我们无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,我们...

  • 手势识别

    手势识别 6种手势识别 在iOS开发中有6中手势识别:点按、捏合、拖动、轻扫、旋转、长按苹果推出手势识别主要是为了...

  • UIGestureRecognizer

    什么是手势识别器? 手势识别器就是对触摸事件做了封装,我们不需要判断某个手势是否触发,手势识别器本身起到了识别作用...

  • 手势——UIGestureRecognizer

    一、简介 UIGestureRecognizer是具体手势识别器的基类。 手势识别器对象(或简单地说是手势识别器)...

  • EasyAR手势姿势识别

    简介 简单试了下EasyAR的手势识别以及姿势识别 手势识别 目前官方是有两种手势 测试了感觉识别度蛮高的 也蛮快...

  • iOS手势识别

    UIGestureRecognizer手势识别器手势识别器是特殊的触摸事件UIGestureRecognizer是...

  • UIGestureRecognizer手势识别器学习笔记

    UIGestureRecognizer 具体手势识别器的基类。一个手势识别器对象,或者简单地说一个手势识别器,解耦...

  • ios多手势传递,用于页面头部悬浮滚动

    /* 是否允许多个手势识别器共同识别,一个控件的手势识别后是否阻断手势识别继续向下传播,默认返回NO;如果为YES...

网友评论

    本文标题:手势识别

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