手势识别 ios

 1 /**
 2  UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中手势。
 3  UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,他有下面一些子类用于处理具体的手势:
 4  1.拍击UITapGestureRecognizer(任意次数的拍击)
 5  2.向里或向外捏 UIPinchGestureRecognizer(用于缩放)
 6  3.摇动或者拖拽 UIPanGestureRecognizer
 7  4.滑动 UISwipeGestureRecoginzer(以任意方向)
 8  5.旋转 UIRotationGestureRecoginzer (手指朝相反方向移动)
 9  6.长按 UILongPressGestureRecoginzer
10  
11  对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一 个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。下面列举几个具体示例代码:
12  */
13 
14 
15 #pragma mark 一个手指,拍击两次手势
16 -(void)test1{
17     UITapGestureRecognizer *oneFingerTwoTaps =  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(oneFingerTwoTaps)];
18     // Set required taps and number of touches
19     //敲击次数 随意设,不过一般情况都是不多于2此
20     [oneFingerTwoTaps setNumberOfTapsRequired:2];
21     //敲击手指数 随意设,不过要考虑用户使用
22     [oneFingerTwoTaps setNumberOfTouchesRequired:1];
23     
24     // Add the gesture to the view
25     //添加到View
26     [self.view addGestureRecognizer:oneFingerTwoTaps];
27 }
28 //消息方法
29 -(void)oneFingerTwoTaps
30 {
31     NSLog(@"敲击了。。。");
32 }
33 
34 #pragma mark 一个手指滑动手势
35 -(void)test2
36 {
37     UISwipeGestureRecognizer *oneFingerSwipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(oneFingerSwipeUp:)];
38     
39     
40     //滑动方向<#(UISwipeGestureRecognizerDirection)#>枚举类型
41     /*
42      UISwipeGestureRecognizerDirectionRight = 1 << 0,
43      UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
44      UISwipeGestureRecognizerDirectionUp    = 1 << 2,
45      UISwipeGestureRecognizerDirectionDown  = 1 << 3
46      */
47     [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp ];
48     [self.view addGestureRecognizer:oneFingerSwipeUp];
49 }
50 //消息方法
51 -(void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
52 {
53     CGPoint point = [recognizer locationInView:self.view];
54    NSLog(@"上滑 - start location: %f,%f", point.x, point.y);
55 }
56 
57 #pragma mark 旋转手势
58 -(void)test3
59 {
60     UIRotationGestureRecognizer *twoFingersRoatate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(twoFingersRotate:)];
61     [self.view addGestureRecognizer:twoFingersRoatate];
62     
63 }
64 -(void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
65 {
66     NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
67 }

 

posted @ 2015-09-10 20:53  海东青飞吧!  阅读(123)  评论(0编辑  收藏  举报