[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer

A.系统提供的手势识别器
技术分享
 
1.敲击手势 UITapGestureRecognizer
numberOfTapsRequired: 敲击次数
numberOfTouchesRequired: 同时敲击触碰数(手指数)
 1 - (void) testTap {
 2     // 创建手势识别器
 3     UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)];
 4     tapRec.numberOfTapsRequired = 3; // 触发需要点击的次数
 5     tapRec.numberOfTouchesRequired = 2; // 触发需要同时点击的点数目
 6    
 7     // 配置手势识别器到控件
 8     [self.hvwView addGestureRecognizer: tapRec];
 9 }
10 
11 /** tap手势的事件处理方法 */
12 - (void) tapRun:(UITapGestureRecognizer *) tapRec {
13     NSLog(@"tapRun");
14 }

 

2.长按手势 UILongPressGestureRecognizer
minimumPressDuration: 长按生效时间
numberOfTouchesRequired: 需要的同时点击数
numberOfTapsRequired: 需要的点击次数
allowableMovement: 长按有效移动范围(从点击开始,长按移动的允许范围)
ps:移动的时候会不断调用目标方法
1 - (void) testLongPress {
2     UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)];
3    
4     longRec.minimumPressDuration = 2; // 长按生效时间
5     longRec.allowableMovement = 50; // 长按允许移动范围,单位:px
6    
7     [self.hvwView addGestureRecognizer:longRec];
8 }
 
3.轻扫手势 UISwipeGestureRecognizer
direction:扫的方向 上下左右
1 typedef enum {
2    UISwipeGestureRecognizerDirectionRight = 1 << 0,
3    UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
4    UISwipeGestureRecognizerDirectionUp    = 1 << 2,
5    UISwipeGestureRecognizerDirectionDown  = 1 << 3
6 } UISwipeGestureRecognizerDirection;
 
1 - (void) testSwipe {
2     UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)];
3     swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向
4    
5     [self.hvwView addGestureRecognizer:swipeRec];
6 }

 

4.捏合手势 UIPinchGestureRecognizer
scale: 捏合的距离
 
 1 - (void) testPinch {
 2     UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)];
 3    
 4     [self.hvwView addGestureRecognizer:rec];
 5 }
 6 
 7 - (void) pinchRun:(UIPinchGestureRecognizer *) rec {
 8     rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale);
 9    
10     // 一定要复位!!!不然按住捏合会不断叠加倍数
11     rec.scale = 1;
12 }
 
技术分享  技术分享
 
5.旋转手势 UIRotationGestureRecognizer
rotation: 旋转了的角度
 1 - (void) testRotation {
 2     UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)];
 3    
 4     [self.hvwView addGestureRecognizer:rotationRec];
 5 }
 6 
 7 - (void) rotationRun:(UIRotationGestureRecognizer *) rec {
 8     rec.view.transform = CGAffineTransformRotate(rec.view.transform, rec.rotation);
 9    
10     // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation
11     rec.rotation = 0;
12 }
 
技术分享
 
6.拖曳手势 UIPanGestureRecognizer
相对始点拖曳距离:[pan translationInView:pan.view]
 1 - (void) testPan {
 2     UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)];
 3    
 4     [self.hvwView addGestureRecognizer:rec];
 5 }
 6 
 7 - (void) panRun:(UIPanGestureRecognizer *) rec {
 8     // 取得拖曳距离
 9     CGPoint movedDistance = [rec translationInView:rec.view];
10     CGPoint viewCenter = rec.view.center;
11     viewCenter.x += movedDistance.x;
12     viewCenter.y += movedDistance.y;
13     rec.view.center = viewCenter;
14    
15     // 复位拖曳距离
16     [rec setTranslation:CGPointZero inView:rec.view];
17 }
 
 
技术分享
 
 
B.一般情况只能使用一个识别器,使用代理设置才能同时使用多个识别器
技术分享
 
1.遵守协议:
1 @interface ViewController () <UIGestureRecognizerDelegate>
 
2.给需要同时进行识别的手势识别器设置代理
1 rec.delegate = self;
 
3.实现手势识别器同时使用方法
1 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
2    
3     // 任何两种手势识别器都能同时使用
4     return YES;
5 }
 
  1 //
  2 //  ViewController.m
  3 //  GestureRecognizerTest
  4 //
  5 //  Created by hellovoidworld on 15/1/13.
  6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @interface ViewController () <UIGestureRecognizerDelegate>
 12 
 13 @property (weak, nonatomic) IBOutlet UIView *hvwView;
 14 
 15 @end
 16 
 17 @implementation ViewController
 18 
 19 - (void)viewDidLoad {
 20     [super viewDidLoad];
 21     // Do any additional setup after loading the view, typically from a nib.
 22    
 23 //    [self testTap];
 24 //    [self testLongPress];
 25 //    [self testSwipe];
 26     [self testRotation];
 27     [self testPinch];
 28     [self testPan];
 29 }
 30 
 31 - (void) testTap {
 32     // 创建手势识别器
 33     UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)];
 34     tapRec.numberOfTapsRequired = 3; // 触发需要点击的次数
 35     tapRec.numberOfTouchesRequired = 2; // 触发需要同时点击的点数目
 36    
 37     // 配置手势识别器到控件
 38     [self.hvwView addGestureRecognizer: tapRec];
 39 }
 40 
 41 /** tap手势的事件处理方法 */
 42 - (void) tapRun:(UITapGestureRecognizer *) tapRec {
 43     NSLog(@"tapRun");
 44 }
 45 
 46 - (void) testLongPress {
 47     UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)];
 48    
 49     longRec.minimumPressDuration = 2; // 长按生效时间
 50     longRec.allowableMovement = 50; // 长按允许移动范围,单位:px
 51    
 52     [self.hvwView addGestureRecognizer:longRec];
 53 }
 54 
 55 - (void) longPressRun:(UILongPressGestureRecognizer *) rec {
 56     NSLog(@"longPress");
 57 }
 58 
 59 - (void) testSwipe {
 60     UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)];
 61     swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向
 62    
 63     [self.hvwView addGestureRecognizer:swipeRec];
 64 }
 65 
 66 - (void) swipeRun:(UISwipeGestureRecognizer *) rec {
 67     NSLog(@"swipe");
 68 }
 69 
 70 - (void) testRotation {
 71     UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)];
 72     rotationRec.delegate = self;
 73    
 74     [self.hvwView addGestureRecognizer:rotationRec];
 75 }
 76 
 77 - (void) rotationRun:(UIRotationGestureRecognizer *) rec {
 78     rec.view.transform = CGAffineTransformRotate(self.hvwView.transform, rec.rotation);
 79    
 80     // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation
 81     rec.rotation = 0;
 82 }
 83 
 84 - (void) testPinch {
 85     UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)];
 86     rec.delegate = self;
 87    
 88     [self.hvwView addGestureRecognizer:rec];
 89 }
 90 
 91 - (void) pinchRun:(UIPinchGestureRecognizer *) rec {
 92     rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale);
 93    
 94     // 一定要复位!!!不然按住捏合会不断叠加倍数
 95     rec.scale = 1;
 96 }
 97 
 98 - (void) testPan {
 99     UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)];
100     rec.delegate = self;
101    
102     [self.hvwView addGestureRecognizer:rec];
103 }
104 
105 - (void) panRun:(UIPanGestureRecognizer *) rec {
106     // 取得拖曳距离
107     CGPoint movedDistance = [rec translationInView:rec.view];
108     CGPoint viewCenter = rec.view.center;
109     viewCenter.x += movedDistance.x;
110     viewCenter.y += movedDistance.y;
111     rec.view.center = viewCenter;
112    
113     // 复位拖曳距离
114     [rec setTranslation:CGPointZero inView:rec.view];
115 }
116 
117 - (void)didReceiveMemoryWarning {
118     [super didReceiveMemoryWarning];
119     // Dispose of any resources that can be recreated.
120 }
121 
122 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
123    
124     // 任何两种手势识别器都能同时使用
125     return YES;
126 }
127 
128 @end
 
#mark:
因为使用的是transform形变进行缩放、旋转,所以再进行拖曳的时候是不能正常进行的。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。