UITapGestureRecognizer停止触摸已结束 [英] UITapGestureRecognizer Stops touchesEnded

查看:105
本文介绍了UITapGestureRecognizer停止触摸已结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个由触摸控制的iPhone应用程序。我还希望在用户双击时激活开机。这是我到目前为止的内容:

I'm trying to make an iPhone app that is controlled by touch. I also want a powerup to be activated when the user double-taps. Here's what I have so far:

UITapGestureRecognizer *powerRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(usePower)];
powerRecognizer.delaysTouchesEnded = NO;
powerRecognizer.numberOfTapsRequired = 2;
powerRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:powerRecognizer];
[powerRecognizer release];

但是问题是,当我双击时,我的 touchesEnded:withEvent :方法仅触发一次,但是我的 touchesBegan:withEvent:方法仅触发两次。由于 touchesBegan:设置了一个计时器,并且 touchesEnded:使它无效,所以当 touchesEnded:仅触发一次,计时器仍在运行。我该如何解决?

But the problem is, when I double-tap, my touchesEnded:withEvent: method only fires once, but my touchesBegan:withEvent: method fires twice. Since touchesBegan: sets a timer and touchesEnded: invalidates it, then when touchesEnded: only fires once, the timer is still running. How can I fix this?

推荐答案

这是我检测双抽头的解决方案:

Here is my solution for detecting double-taps:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{   

UITouch *touch = [touches anyObject];

if([touch tapCount] == 2) {
// do sth   
}

}

这篇关于UITapGestureRecognizer停止触摸已结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆