iphone开发中的手势操作:Multiple Taps

简介:

用Automatic Gesture Recognition实现: 

在viewDidLoad中

复制代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap1)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap2)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];

UITapGestureRecognizer *tripleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap3)];
tripleTap.numberOfTapsRequired = 3;
tripleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tripleTap];
[doubleTap requireGestureRecognizerToFail:tripleTap];

UITapGestureRecognizer *quadrupleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap4)];
quadrupleTap.numberOfTapsRequired = 4;
quadrupleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:quadrupleTap];
[tripleTap requireGestureRecognizerToFail:quadrupleTap];
}
复制代码

添加自定义响应方法:

复制代码
- (void)tap1 {
singleLabel.text = @"Single Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:singleLabel afterDelay:1.6f];
}

- (void)tap2 {
doubleLabel.text = @"Double Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:doubleLabel afterDelay:1.6f];
}

- (void)tap3 {
tripleLabel.text = @"Triple Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:tripleLabel afterDelay:1.6f];
}

- (void)tap4 {
quadrupleLabel.text = @"Quadruple Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:quadrupleLabel afterDelay:1.6f];
}

- (void)eraseMe:(UILabel *)label {
label.text = @"";
}
复制代码

其实也可以直接在touchesBegan中自己实现功能:

复制代码
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
NSUInteger tapCout = [touch tapCount];

switch (tapCout) {
case 1:
[self performSelector:@selector(tap1) withObject:nil afterDelay:.4];//延迟执行
break;
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap1) object:nil];//取消tap1
[self performSelector:@selector(tap2) withObject:nil afterDelay:.4];
break;
case 3:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap2) object:nil];//取消tap2
[self performSelector:@selector(tap3) withObject:nil afterDelay:.4];
break;
case 4:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap3) object:nil];//取消tap3
[self tap4];
break;
default:
break;
}
}
复制代码



that‘s all.

本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/03/26/2417513.html,如需转载请自行联系原作者

相关文章
|
29天前
|
编解码 测试技术 iOS开发
iPhone 屏幕尺寸和开发适配
【10月更文挑战第23天】iPhone 的屏幕尺寸变化给开发者带来了一定的挑战,但也为创新提供了机遇。通过深入了解不同屏幕尺寸的特点,遵循适配原则和策略,运用合适的技巧和方法,我们能够为用户提供在不同 iPhone 机型上都具有良好体验的应用。在未来,随着技术的不断进步,我们还需要持续学习和适应,以满足用户对优质应用体验的不断追求。
|
29天前
|
编解码 iOS开发 UED
响应式设计在 iPhone 开发适配中的具体应用
【10月更文挑战第23天】响应式设计在 iPhone 开发适配中扮演着至关重要的角色,它能够帮助我们打造出适应不同屏幕尺寸和用户需求的高质量应用。通过合理运用响应式设计的原则和方法,我们可以在提供良好用户体验的同时,提高开发效率和应用的可维护性。
|
4月前
|
数据采集 iOS开发 Python
Chatgpt教你开发iPhone风格计算器,Python代码实现
Chatgpt教你开发iPhone风格计算器,Python代码实现
|
Shell iOS开发
iOS逆向:tweak开发教程(iPhone/tool)
iOS逆向:tweak开发教程(iPhone/tool)
1199 0
iOS逆向:tweak开发教程(iPhone/tool)
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
233 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
269 0
|
Web App开发 缓存 开发工具
|
存储 iOS开发 计算机视觉
|
Web App开发 前端开发 JavaScript