UITapGestureRecognizer 的用法

最近在项目中用到了手势操作,键盘回收时还是挺常用的,现在总结下,多谢网络上大神们的分享。

先分享下我在项目中用的代码:

复制代码
 1 UITapGestureRecognizer * mytap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap_gestureRecognizer:)];
 2     [self addGestureRecognizer:mytap];
 3     [mytap release];
 4 
 5 
 6 //收回键盘
 7 -(void)tap_gestureRecognizer:(UITapGestureRecognizer *)tap_gest
 8 {
 9     [self.scv_stockPriceTextField resignFirstResponder];
10     [self.scv_stockAmoutTextField resignFirstResponder];
11 
12 }
复制代码

 

 
 

//单指单击
2: UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
3: action:@selector(handleSingleFingerEvent:)];
4: singleFingerOne.numberOfTouchesRequired = 1; //手指数
5: singleFingerOne.numberOfTapsRequired = 1; //tap次数
6: singleFingerOne.delegate = self;
7:
8: //单指双击
9: UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
10: action:@selector(handleSingleFingerEvent:)];
11: singleFingerTwo.numberOfTouchesRequired = 1;
12: singleFingerTwo.numberOfTapsRequired = 2;
13: singleFingerTwo.delegate = self;
14:
15: //双指单击
16: UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
17: action:@selector(handleDoubleFingerEvent:)];
18: doubleFingerOne.numberOfTouchesRequired = 2;
19: doubleFingerOne.numberOfTapsRequired = 1;
20: doubleFingerOne.delegate = self;
21:
22: UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
23:    action:@selector(handleDoubleFingerEvent:)];
24: doubleFingerTwo.numberOfTouchesRequired = 2;
25: doubleFingerTwo.numberOfTapsRequired = 2;
26: doubleFingerTwo.delegate = self;
27:
28: //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
29: [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
30: //同理双指亦是如此
31: [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
32:
33: [self.view addGestureRecognizer:singleFingerOne];
34: [self.view addGestureRecognizer:singleFingerTwo];
35: [self.view addGestureRecognizer:doubleFingerOne];
36: [self.view addGestureRecognizer:doubleFingerTwo];
37:
38: [singleFingerOne release];
39: [singleFingerTwo release];
40: [doubleFingerOne release];
41: [doubleFingerTwo release];

处理事件的方法,代码:

1: //处理单指事件
2: - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
3: {
4: if (sender.numberOfTapsRequired == 1) {
5: //单指单击
6: NSLog(@"单指单击");
7: }else if(sender.numberOfTapsRequired == 2){
8: //单指双击
9: NSLog(@"单指双击");
10: }
11: }
12: //处理双指事件
13: - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
14: {
15: if (sender.numberOfTapsRequired == 1) {
16: //双指单击
17: NSLog(@"双指单击");
18: }else if(sender.numberOfTapsRequired == 2){
19: //双指双击
20: NSLog(@"双指双击");
21: }
22: }

将相应代码复制到你的工程中即可使用,由于代码中已经有详细的解释说明,这里就不在重复解释了。

代码中只是列举了单指与双指对于单击或多击的处理,同理多指的操作需修改numberOfTouchesRequired属性,对点击的次数需修改numberOfTapsRequired属性。

对于其他手势例如UISwipeGestureRecognizerUILongPressGestureRecognizer,UILongPressGestureRecognizer的操作使用类似处理。

 


 

UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势: 

    1、拍击UITapGestureRecognizer (任意次数的拍击)  
    2、向里或向外捏UIPinchGestureRecognizer (用于缩放)  
    3、摇动或者拖拽UIPanGestureRecognizer  
    4、擦碰UISwipeGestureRecognizer (以任意方向)  
    5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)  
    6、长按UILongPressGestureRecognizer 

对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一 个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。下面列举几个具体示例代码: 
http://www.ctolive.com/space-1023-do-blog-id-2107.html 
 
标签: Objective-C iOS
 

代码片段(5)

[代码] 一个手指,拍击两次手势

01// 创建一个手势识别器
02UITapGestureRecognizer *oneFingerTwoTaps =
03  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
04  
05// Set required taps and number of touches
06[oneFingerTwoTaps setNumberOfTapsRequired:2];
07[oneFingerTwoTaps setNumberOfTouchesRequired:1];
08  
09// Add the gesture to the view
10[[self view] addGestureRecognizer:oneFingerTwoTaps];
11
12消息方法oneFingerTwoTaps
13- (void)oneFingerTwoTaps
14{
15  NSLog(@"Action: One finger, two taps");
16}

[代码] 两个手指,拍击两次手势

01UITapGestureRecognizer *twoFingersTwoTaps =
02  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
03[twoFingersTwoTaps setNumberOfTapsRequired:2];
04[twoFingersTwoTaps setNumberOfTouchesRequired:2];
05[[self view] addGestureRecognizer:twoFingersTwoTaps];
06
07消息方法twoFingersTwoTaps
08- (void)twoFingersTwoTaps {
09  NSLog(@"Action: Two fingers, two taps");
10}

[代码] 一个手指向上、向下擦碰手势

01// 向上擦碰
02UISwipeGestureRecognizer *oneFingerSwipeUp =
03  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
04[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
05[[self view] addGestureRecognizer:oneFingerSwipeUp];
06
07- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
08{
09  CGPoint point = [recognizer locationInView:[self view]];
10  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
11}
12
13// 向下擦碰
14UISwipeGestureRecognizer *oneFingerSwipeDown =
15  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
16[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
17[[self view] addGestureRecognizer:oneFingerSwipeDown];
18
19- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
20{
21  CGPoint point = [recognizer locationInView:[self view]];
22  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
23}

[代码] 旋转手势

1UIRotationGestureRecognizer *twoFingersRotate =
2  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
3[[self view] addGestureRecognizer:twoFingersRotate];
4
5- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
6{
7  // Convert the radian value to show the degree of rotation
8  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
9}

[代码] 向里或向外捏的手势

1UIPinchGestureRecognizer *twoFingerPinch =
2  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
3[[self view] addGestureRecognizer:twoFingerPinch];
4
5- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
6{
7

  NSLog(@"Pinch scale: %f", recognizer.scale);

}


在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:

  • UITapGestureRecognizer
  • UIPinchGestureRecognizer
  • UIRotationGestureRecognizer
  • UISwipeGestureRecognizer
  • UIPanGestureRecognizer
  • UILongPressGestureRecognizer

从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。

复制代码
复制代码
// 定义一个 recognizer, 并加到需要偵測该手势的 UIView 元件上
- (void)viewDidLoad {
UISwipeGestureRecognizer* recognizer;
// handleSwipeFrom 是偵測到手势,所要呼叫的方法
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom)];
// 不同的 Recognizer 有不同的实体变数
// 例如 SwipeGesture 可以指定方向
// 而 TapGesture 則可以指定次數
recognizer.direction = UISwipeGestureRecognizerDirectionUp
[self.view addGestureRecognizer:recognizer];
[recognizer release];
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
// 触发手勢事件后,在这里作些事情

// 底下是刪除手势的方法
[self.view removeGestureRecognizer:recognizer];
}
复制代码
复制代码
问题來了。有些手势其实是互相关联的,例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。当一個 UIView 同时添加兩个相关联的手势时,到底我这一下手指头按的要算是 Tap 还是 LongPress?如果照預设作法来看,只要「先滿足条件」的就会跳出并呼叫对应方法,举例来说,如果同时注册了 Pan 和 Swipe,只要手指头一移动就会触发 Pan 然后跳出,因而永远都不會发生 Swipe;单点与双点的情形也是一样,永远都只会触发单点,不會有双点。

那么这个问题有解吗?答案是肯定的,UIGestureRecognizer 有个方法叫做requireGestureRecognizerToFail,他可以指定某一个 recognizer,即便自己已经滿足條件了,也不會立刻触发,会等到该指定的 recognizer 确定失败之后才触发。以同时支持单点与双点的手势为例,代码如下:

复制代码
复制代码
- (void)viewDidLoad {
// 单击的 Recognizer
UITapGestureRecognizer* singleRecognizer;
singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)];
singleTapRecognizer.numberOfTapsRequired = 1; // 单击
[self.view addGestureRecognizer:singleRecognizer];

// 双击的 Recognizer
UITapGestureRecognizer* double;
doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)];
doubleTapRecognizer.numberOfTapsRequired = 2; // 双击
[self.view addGestureRecognizer:doubleRecognizer];

// 关键在这一行,如果双击确定偵測失败才會触发单击
[singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];
[singleRecognizer release];
[doubleRecognizer release];
}
复制代码
 

来源:http://blog.csdn.net/xiaoxuan415315/article/details/7788239



本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3469857.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值