iOS开发教程之手势识别方法

移动开发 iOS
感觉有必要把iOS开发中的手势识别做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextView中的手是用storyboard添加的。下面会先给出如何用storyboard给相应的控件添加手势,然后在用纯代码的方式给我们的控件添加手势,手势的用法比较简单。和button的用法类似,

感觉有必要把iOS开发中的手势识别做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextView中的手是用storyboard添加的。下面会先给出如何用storyboard给相应的控件添加手势,然后在用纯代码的方式给我们的控件添加手势,手势的用法比较简单。和button的用法类似,也是目标动作回调,话不多说,切入今天的正题。总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势(SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer), 拖动手势(PanGestureRecognizer), 捏合手势(PinchGestureRecognizer),旋转手势(RotationGestureRecognizer);

其实这些手势用touche事件完全可以实现,苹果就是把常用的触摸事件封装成手势,来提供给用户。读者完全可以用TouchesMoved来写拖动手势等

一,用storyboard给控件添加手势识别,当然啦用storyboard得截张图啦

1.用storyboard添加手势识别,和添加一个Button的步骤一样,首先我们得找到相应的手势,把手势识别的控件拖到我们要添加手势的控件中,截图如下

2.给我们拖出的手势添加回调事件,和给Button回调事件没啥区别的,在回调方法中添加要实现的业务逻辑即可,截图如下:

 二,纯代码添加手势识别

用storyboard可以大大简化我们的操作,不过纯代码的方式还是要会的,就像 要Dreamwear编辑网页一样(当然啦,storyboard的拖拽功能要比Dreamwear的拖拽强大的多),用纯代码敲出来的更为灵活,更加便 于维护。不过用storyboard可以减少我们的工作量,这两个要配合着使用才能大大的提高我们的开发效率。个人感觉用storyboard把框架搭起 来(Controller间的关系),一下小的东西还是用纯代码敲出来更好一些。下面就给出如何给我们的控件用纯代码的方式来添加手势识别。

1.轻击手势(TapGestureRecognizer)的添加

初始化代码TapGestureRecongnizer的代码如下:

  1. 1     //新建tap手势 
  2. 2     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; 
  3. 3     //设置点击次数和点击手指数 
  4. 4     tapGesture.numberOfTapsRequired = 1//点击次数 
  5. 5     tapGesture.numberOfTouchesRequired = 1//点击手指数 
  6. 6     [self.view addGestureRecognizer:tapGesture]; 

在回调方法中添加相应的业务逻辑:

  1. 1 //轻击手势触发方法 
  2. 2 -(void)tapGesture:(id)sender 
  3. 3 { 
  4. 4     //轻击后要做的事情         
  5. 5 } 

2.长按手势(LongPressGestureRecognizer)

初始化代码:

  1. //添加长摁手势 
  2. 2     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)]; 
  3. 3     //设置长按时间 
  4. 4     longPressGesture.minimumPressDuration = 0.5//(2秒) 
  5. 5     [self.view addGestureRecognizer:longPressGesture]; 

在对应的回调方法中添加相应的方法(当手势开始时执行):

  1. 1 //常摁手势触发方法 
  2. 2 -(void)longPressGesture:(id)sender 
  3. 3 { 
  4. 4     UILongPressGestureRecognizer *longPress = sender; 
  5. 5     if (longPress.state == UIGestureRecognizerStateBegan) 
  6. 6     { 
  7. 7         UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按触发" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil]; 
  8. 8         [alter show]; 
  9. 9     } 
  10. 0 } 

代码说明:手势的常用状态如下

开始:UIGestureRecognizerStateBegan

改变:UIGestureRecognizerStateChanged

结束:UIGestureRecognizerStateEnded

取消:UIGestureRecognizerStateCancelled

失败:UIGestureRecognizerStateFailed

3.轻扫手势(SwipeGestureRecognizer)

在初始化轻扫手势的时候得指定轻扫的方向,上下左右。 如果要要添加多个轻扫方向,就得添加多个轻扫手势,不过回调的是同一个方法。

添加轻扫手势,一个向左一个向右,代码如下:

  1. 1     //添加轻扫手势 
  2.  2     UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; 
  3.  3     //设置轻扫的方向 
  4.  4     swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默认向右 
  5.  5     [self.view addGestureRecognizer:swipeGesture]; 
  6.  6      
  7.  7     //添加轻扫手势 
  8.  8     UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; 
  9.  9     //设置轻扫的方向 
  10. 10     swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默认向右 
  11. 11     [self.view addGestureRecognizer:swipeGestureLeft]; 

回调方法如下:

  1. 1 //轻扫手势触发方法 
  2.  2 -(void)swipeGesture:(id)sender 
  3.  3 { 
  4.  4     UISwipeGestureRecognizer *swipe = sender; 
  5.  5     if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) 
  6.  6     { 
  7.  7         //向左轻扫做的事情 
  8.  8     } 
  9.  9     if (swipe.direction == UISwipeGestureRecognizerDirectionRight) 
  10. 10     { 
  11. 11         //向右轻扫做的事情 
  12. 12     } 
  13. 13 } 
  14. 14      

4.捏合手势(PinchGestureRecognizer)

捏合手势初始化

  1.   //添加捏合手势 
  2. 2     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)]; 
  3. 3     [self.view addGestureRecognizer:pinchGesture]; 

捏合手势要触发的方法(放大或者缩小图片):

  1.  1 ////捏合手势触发方法 
  2.  2 -(void) pinchGesture:(id)sender 
  3.  3 { 
  4.  4      UIPinchGestureRecognizer *gesture = sender; 
  5.  5      
  6.  6     //手势改变时 
  7.  7     if (gesture.state == UIGestureRecognizerStateChanged) 
  8.  8     { 
  9.  9         //捏合手势中scale属性记录的缩放比例 
  10. 10         _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale); 
  11. 11     } 
  12. 12      
  13. 13     //结束后恢复 
  14. 14     if(gesture.state==UIGestureRecognizerStateEnded) 
  15. 15     { 
  16. 16         [UIView animateWithDuration:0.5 animations:^{ 
  17. 17             _imageView.transform = CGAffineTransformIdentity;//取消一切形变 
  18. 18         }]; 
  19. 19     } 
  20. 20 } 

5.拖动手势(PanGestureRecognizer)

拖动手势的初始化

  1. //添加拖动手势 
  2. 2     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; 
  3. 3     [self.view addGestureRecognizer:panGesture]; 

拖动手势要做的方法(通过translationInView获取移动的点,和TouchesMoved方法类似)

  1. 1 //拖动手势 
  2. 2 -(void) panGesture:(id)sender 
  3. 3 { 
  4. 4     UIPanGestureRecognizer *panGesture = sender; 
  5. 5      
  6. 6     CGPoint movePoint = [panGesture translationInView:self.view]; 
  7. 7      
  8. 8     //做你想做的事儿 
  9. 9 } 

6.旋转手势(RotationGestureRecognizer)

旋转手势的初始化

  1.  //添加旋转手势 
  2. 2     UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)]; 
  3. 3     [self.view addGestureRecognizer:rotationGesture]; 

旋转手势调用的方法:

  1. 复制代码 
  2.  
  3.  1 //旋转手势 
  4.  2 -(void)rotationGesture:(id)sender 
  5.  3 { 
  6.  4      
  7.  5     UIRotationGestureRecognizer *gesture = sender; 
  8.  6      
  9.  7     if (gesture.state==UIGestureRecognizerStateChanged) 
  10.  8     { 
  11.  9         _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation); 
  12. 10     } 
  13. 11      
  14. 12     if(gesture.state==UIGestureRecognizerStateEnded) 
  15. 13     { 
  16. 14          
  17. 15         [UIView animateWithDuration:1 animations:^{ 
  18. 16             _imageView.transform=CGAffineTransformIdentity;//取消形变 
  19. 17         }]; 
  20. 18     } 
  21. 19      
  22. 20 } 

本文链接:http://www.cnblogs.com/ludashi/p/3983008.html

责任编辑:chenqingxiang 来源: cnblogs
相关推荐

2022-08-16 15:20:12

微服务IT运维

2022-05-17 12:25:59

物联网智能建筑楼宇自控

2011-06-30 10:49:27

2011-06-30 10:36:22

JSF

2018-04-02 10:45:11

深度学习PaddlePaddl手写数字识别

2020-04-10 14:10:50

人脸识别人工智能华为

2013-05-07 17:21:09

ELMOS芯片手势识别

2010-11-01 09:46:21

ViewAndroid

2021-08-13 10:01:19

人脸识别人工智能数据

2013-05-21 11:20:37

Android游戏开发View手势识别

2013-06-20 10:50:51

Objective-CiOS左右滑动手势

2011-07-08 14:58:16

iPhone Xcode iOS

2009-06-11 19:54:19

Hibernate入门

2012-02-28 14:07:17

Android触摸屏手势识别

2022-03-28 07:52:31

H5小游戏开发教程页面基础布局

2011-07-12 17:11:13

PHPSHELL

2023-07-06 08:41:20

TTS​Mac​系统

2013-05-02 11:21:36

iOS开发流程

2011-07-22 18:13:59

IOS IDE Xcode

2011-12-30 15:17:23

Adobe视频PhoneGap
点赞
收藏

51CTO技术栈公众号