博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone开发中的手势操作:Multiple Taps
阅读量:6172 次
发布时间:2019-06-21

本文共 2900 字,大约阅读时间需要 9 分钟。

用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,如需转载请自行联系原作者

你可能感兴趣的文章
hausaufgabe--python 36-- Basic knowledge of Class
查看>>
redis 常用命令
查看>>
一个Pan&Zoom的Behavior
查看>>
062:ORM查询条件详解-exact和iexact
查看>>
【loj3056】【hnoi2019】多边形
查看>>
C++:sprintf()的用法
查看>>
Git-pull进入vim窗口解决办法
查看>>
简单操作
查看>>
添加源,删除源
查看>>
今天的作业
查看>>
day056 多表增加和查询
查看>>
C++——多态性 与 虚函数
查看>>
Beta 冲刺(6/7)
查看>>
错题集
查看>>
libev客户端
查看>>
VM连接方式
查看>>
git 命令
查看>>
【js高三】---js模块模式
查看>>
IOS开发-UIView之动画效果的实现方法(合集)
查看>>
Apache端口配置
查看>>