iOS触摸手势——UIGestureRecognizer

Demo实例


UIGestureRecognizer类,用于检测发生在设备中的手势。

UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势: 

1、拍击 UITapGestureRecognizer (任意次数的拍击)  

通过设置属性" numberOfTapsRequired "实现单击,或双击


2、向里或向外捏 UIPinchGestureRecognizer (用于缩放)  

通过设置属性" scale "实现对UI控件大小进行缩放的改变,如:

float lastScale = 0.0;

UILabel *view = (UILabel *)recognizer.view;

recognizer.scale = recognizer.scale - lastScale + 1.0;

view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale);

lastScale = recognizer.scale;


3、摇动或者拖拽 UIPanGestureRecognizer  


4、滑动 UISwipeGestureRecognizer (以任意方向)  

通过设置属性" direction "实现滑动方向


5、旋转 UIRotationGestureRecognizer (手指朝相反方向移动)  

通过设置属性" rotation "实现对UI控件旋转的改变,如:

UILabel *view = (UILabel *)recognizer.view;

float rotationV = recognizer.rotation;

view.transform = CGAffineTransformMakeRotation(rotationV);


6、长按 UILongPressGestureRecognizer 

通过设置属性" longpressRecognizer "实现长按响应时间


对于不同类型的手势识别器,具有不同的配置属性。

比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一 个消息,用于处理响应手势动作后的任务。

不同的手势识别器,发送的消息方法也会有所不同。

使用注意事项:

1、实例化时,同时设置响应target对象,以及响应方法

2、响应方法中,可能会出现方法被多次响应的情况,可通过设置手势状态进行控制



[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 图像视图  
  2.  UIImageView *recognizerImage = [[UIImageView alloc] initWithFrame:CGRectMake(101446060)];  
  3.  [recognizerImage setImage:[UIImage imageNamed:@"dingdang.png"]];  
  4.  [recognizerImage setBackgroundColor:[UIColor clearColor]];  
  5.  [recognizerImage setUserInteractionEnabled:YES];  
  6.  [self.view addSubview:recognizerImage];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 点击手势  
  2. // 单击的手势  
  3. UITapGestureRecognizer *tapRecognizerSingle = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizerSingleAction:)];  
  4. tapRecognizerSingle.numberOfTapsRequired = 1// 单击  
  5. // 图像视图添加单击手势  
  6. [recognizerImage addGestureRecognizer:tapRecognizerSingle];  
  7.       
  8. // 双击的手势  
  9. UITapGestureRecognizer *tapRecognizerDouble = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizerDoubleAction:)];  
  10. tapRecognizerDouble.numberOfTapsRequired = 2// 双击  
  11. // 如果双击确定检测失败触发单击  
  12. [tapRecognizerSingle requireGestureRecognizerToFail:tapRecognizerDouble];  
  13. // 图像视图添加双击手势  
  14. [recognizerImage addGestureRecognizer:tapRecognizerDouble];  
  15.   
  16. // 点击手势方法 单击  
  17. bool isChangeBgColor;  
  18. - (void)tapRecognizerSingleAction:(UITapGestureRecognizer *)recognizer  
  19. {  
  20.     // 单击时改变背景颜色  
  21.     if (1 == recognizer.numberOfTapsRequired)  
  22.     {  
  23.         UIImageView *view = (UIImageView *)recognizer.view;  
  24.           
  25.         if (!isChangeBgColor)  
  26.         {  
  27.             [view setBackgroundColor:[UIColor orangeColor]];  
  28.         }  
  29.         else  
  30.         {  
  31.             [view setBackgroundColor:[UIColor yellowColor]];  
  32.         }  
  33.         isChangeBgColor = !isChangeBgColor;  
  34.     }  
  35. }  
  36.   
  37.   
  38. // 点击手势方法 双击  
  39. bool isChangeSize;  
  40. - (void)tapRecognizerDoubleAction:(UITapGestureRecognizer *)recognizer  
  41. {  
  42.     // 放大或缩小视图  
  43.     if (2 == recognizer.numberOfTapsRequired)  
  44.     {  
  45.         UIImageView *view = (UIImageView *)recognizer.view;  
  46.           
  47.         // 通过isChangeSize值区分放大还是缩小  
  48.         if (!isChangeSize)  
  49.         {  
  50.             // 放大  
  51.             CGRect currentBounds = view.bounds;  
  52.             currentBounds.size.width *= 2;  
  53.             currentBounds.size.height *= 2;  
  54.               
  55.             // 添加动画  
  56.             [UIView animateWithDuration:0.3 animations:^{  
  57.                 view.bounds = currentBounds;  
  58.             }];  
  59.         }  
  60.         else  
  61.         {  
  62.             // 缩小  
  63.             CGRect currentBounds = view.bounds;  
  64.             currentBounds.size.width /= 2;  
  65.             currentBounds.size.height /= 2;  
  66.               
  67.             // 添加动画  
  68.             [UIView animateWithDuration:0.3 animations:^{  
  69.                 view.bounds = currentBounds;  
  70.             }];  
  71.         }  
  72.         isChangeSize = !isChangeSize;  
  73.     }  
  74. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 长按手势  
  2. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRecognizerAction:)];  
  3. // 图像视图添加长按手势  
  4. [recognizerImage addGestureRecognizer:longPressRecognizer];  
  5.   
  6. // 长按手势方法  
  7. - (void)longPressRecognizerAction:(UILongPressGestureRecognizer *)recognizer  
  8. {  
  9.     // 改变选中视图背景色(原始背景色,原始大小)  
  10.     UIImageView *view = (UIImageView *)recognizer.view;  
  11.       
  12.     [view setFrame:CGRectMake(006060)];  
  13.     [view setCenter:CGPointMake(self.view.frame.size.width / 2self.view.frame.size.height / 2)];  
  14.     [view setBackgroundColor:[UIColor clearColor]];  
  15. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 旋转手势  
  2. UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateRecognizerAction:)];  
  3. // 图像视图添加旋转手势  
  4. [recognizerImage addGestureRecognizer:rotateRecognizer];  
  5.   
  6. // 旋转手势方法  
  7. - (void)rotateRecognizerAction:(UIRotationGestureRecognizer *)recognizer  
  8. {  
  9.     // 旋转视图  
  10.     UIImageView *view = (UIImageView *)recognizer.view;  
  11.   
  12.     float rotationV = recognizer.rotation;  
  13.     view.transform = CGAffineTransformMakeRotation(rotationV);  
  14. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 拖动手势  
  2. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognizerAction:)];  
  3. // 图像视图添加拖动手势  
  4. [recognizerImage addGestureRecognizer:panRecognizer];  
  5.   
  6. // 拖动手势方法  
  7. - (void)panRecognizerAction:(UIPanGestureRecognizer *)recognizer  
  8. {  
  9.     // 拖动视图  
  10.     UIImageView *view = (UIImageView *)recognizer.view;  
  11.       
  12.     CGPoint translation = [recognizer translationInView:view];  
  13.     CGFloat centerX = view.center.x + translation.x;  
  14.     CGFloat centerY = view.center.y + translation.y;  
  15.     view.center = CGPointMake(centerX, centerY);  
  16.     [recognizer setTranslation:CGPointZero inView:view];  
  17. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 滑动手势  
  2. // 滑动手势-向上滑  
  3. UISwipeGestureRecognizer *swipeRecognizerUP = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];  
  4. swipeRecognizerUP.direction = UISwipeGestureRecognizerDirectionUp;  
  5. // 滑动手势-向下滑  
  6. UISwipeGestureRecognizer *swipeRecognizerDOWN = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];  
  7. swipeRecognizerDOWN.direction = UISwipeGestureRecognizerDirectionDown;  
  8. // 滑动手势-向左滑  
  9. UISwipeGestureRecognizer *swipeRecognizerLEFT = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];  
  10. swipeRecognizerLEFT.direction = UISwipeGestureRecognizerDirectionLeft;  
  11. // 滑动手势-向右滑  
  12. UISwipeGestureRecognizer *swipeRecognizerRIGHT = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];  
  13. swipeRecognizerRIGHT.direction = UISwipeGestureRecognizerDirectionRight;  
  14. // 图像视图添加滑动手势  
  15. [recognizerImage addGestureRecognizer:swipeRecognizerUP];  
  16. [recognizerImage addGestureRecognizer:swipeRecognizerUP];  
  17. [recognizerImage addGestureRecognizer:swipeRecognizerUP];  
  18. [recognizerImage addGestureRecognizer:swipeRecognizerRIGHT];  
  19.   
  20. // 滑动手势方法  
  21. - (void)swipeRecognizerAction:(UISwipeGestureRecognizer *)recognizer  
  22. {  
  23.     UIImageView *view = (UIImageView *)recognizer.view;  
  24.     NSLog(@"view(%@) %@", NSStringFromClass([view class]), view);  
  25.       
  26.     if (recognizer.direction == UISwipeGestureRecognizerDirectionUp)  
  27.     {  
  28.         NSLog(@"direction == UISwipeGestureRecognizerDirectionUp");  
  29.     }  
  30.     else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown)  
  31.     {  
  32.         NSLog(@"direction == UISwipeGestureRecognizerDirectionDown");  
  33.     }  
  34.     else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)  
  35.     {  
  36.         NSLog(@"direction == UISwipeGestureRecognizerDirectionLeft");  
  37.     }  
  38.     else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)  
  39.     {  
  40.         NSLog(@"direction == UISwipeGestureRecognizerDirectionRight");  
  41.     }  
  42. }  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 缩放手势  
  2. UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRecognizerAction:)];  
  3. // 图像视图添加缩放手势  
  4. [recognizerImage addGestureRecognizer:pinchRecognizer];  
  5.   
  6. // 缩放手势方法  
  7. float lastScale = 0.0;  
  8. - (void)pinchRecognizerAction:(UIPinchGestureRecognizer *)recognizer  
  9. {  
  10.     // 缩放视图  
  11.     UIImageView *view = (UIImageView *)recognizer.view;  
  12.       
  13.     recognizer.scale = recognizer.scale - lastScale + 1.0;  
  14.     view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale);  
  15.       
  16.     lastScale = recognizer.scale;  
  17. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值