UITapGestureRecognizer 的用法

简介: 最近在项目中用到了手势操作,键盘回收时还是挺常用的,现在总结下,多谢网络上大神们的分享。 先分享下我在项目中用的代码: 1 UITapGestureRecognizer * mytap=[[UITapGestureRecognizer alloc]initWithTarget:sel...

 

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

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

 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 // 创建一个手势识别器
02 UITapGestureRecognizer *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 }

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

01 UITapGestureRecognizer *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 // 向上擦碰
02 UISwipeGestureRecognizer *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 // 向下擦碰
14 UISwipeGestureRecognizer *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 }

[代码] 旋转手势

1 UIRotationGestureRecognizer *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 }

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

1 UIPinchGestureRecognizer *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

img_e00999465d1c2c1b02df587a3ec9c13d.jpg
微信公众号: 猿人谷
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎关注微信公众号
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

目录
相关文章
|
数据安全/隐私保护
runas的用法
今天同事的电脑安装了一个软件,运行时需要管理员权限,因为是在域环境中,无法提供管理员权限,这种情况可以用到runas。runas命令就是可以在A账户中用B账户运行某个软件。 runas /user:用户名 软件路径 在了解了runas用法后,我先在CMD中输入命令 需要输入账户密码 输入密码成功后就可以运行软件了,检查一下,是以这个用户运行的软件 后来想想运行这个命令每次都要输入管理员密码,这就没什么效果了。
2648 0
|
测试技术 API
harfbuzz 的用法
HarfBuzz 整形 API 的核心是函数。此函数采用一种字体,即 包含一串 Unicode 代码点的缓冲区和 (可选)字体功能列表作为其输入。它取代了 缓冲区中的代码点,其中包含来自 字体,正确排序和定位,以及任何 应用的可选字体功能。hb_shape()
453 0
|
JSON Java fastjson
JackJSON的用法
网上大多数都是FastJSON的用法,很少有JackJSON的用法。我总结了几个自己经常用到的,供大家参考。
mailto用法详解
mailto用法详解
520 0
mailto用法详解
|
存储 API 索引
CImageList用法介绍
CImageList用法介绍
174 0
iTween的用法总结
Unity3D插件-iTween的基本用法 本文提供全流程,中文翻译。Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) 1 ...
1666 0