手势识别

手势识别

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是一个抽象类 , 用于自定义手势 ,不能实例化。


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容