简体   繁体   English

UIButton同时具有Touch Up Inside和Touch Repeat?

[英]UIButton with both Touch Up Inside and Touch Repeat?

What's the best way to get a button to be capable of both a Touch Up Inside action and a Touch Down Repeat action? 让按钮同时具有Touch Up Inside动作和Touch Down Repeat动作的最佳方法是什么? Kind of like the Home button on an iPhone: one click goes to the home screen, but two rapid clicks opens the multitasking bar. 有点像iPhone上的Home按钮:只需点击一下即可进入主屏幕,但两次快速点击可打开多任务栏。 Right now my button has both methods hooked up, but the Touch Up Inside method (unsurprisingly) gets called before the Touch Down Repeat can happen. 现在我的按钮已经连接了两个方法,但Touch Down Repeat Touch Up Inside方法(不出所料)在Touch Down Repeat发生之前被调用。

I can come up with a few ways I might pull this off (having the Touch Up Inside method wait a second to see if another click comes before executing, having a second button move invisibly into place after the first click, etc) but they all seem kind of hack-y and open to performance losses and bugs. 我可以提出一些方法来解决这个问题(让Touch Up Inside方法等待一秒钟,看看在执行之前是否有另一个点击,第二个按钮在第一次点击后无形地移动到位)等等看起来很糟糕,对性能损失和漏洞持开放态度。 I found the tapCount property, but it's for UITouch instead of UIButton, so if([_buttonAddItems tapCount] > 1) {} else if([_buttonAddItems tapCount] == 1) , which seems like it would be the most efficient way of doing it, doesn't work. 我找到了tapCount属性,但是它用于UITouch而不是UIButton,所以if([_buttonAddItems tapCount] > 1) {} else if([_buttonAddItems tapCount] == 1) ,这似乎是最有效的方法它,不起作用。

Is there a best-practice for this sort of thing? 这种事情有最好的做法吗? Or if not, does anyone have a preferred way of getting this done? 或者,如果没有,有没有人有一个首选的方法来完成这项工作?

Try disconnecting both touch events from your button, and hook two tap gesture recognizers to it instead. 尝试断开按钮上的两个触摸事件,并将两个轻触手势识别器挂钩。 Here is a link to an answer that explains how to set up two gesture recognizers so that one of them recognizes a single tap, and the other one recognizes a double tap: 这是一个答案链接 ,解释了如何设置两个手势识别器,以便其中一个识别单击,另一个识别双击:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doSingleTap)];
singleTap.numberOfTapsRequired = 1; 
[myButton addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doDoubleTap)];
doubleTap.numberOfTapsRequired = 2; 
[myButton  addGestureRecognizer:doubleTap];
// This is the "secret sauce":
[singleTap requireGestureRecognizerToFail:doubleTap];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM