UITapGestureRecognizer停止touchesEnded

时间:2010-10-03 21:19:03

标签: iphone objective-c cocoa-touch

我正在尝试制作一款受触控控制的iPhone应用。我还希望在用户双击时激活上电。这是我到目前为止所做的:

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:仅触发一次时,计时器仍在运行。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

向视图添加手势识别器会改变多种触摸处理方法的行为,包括touchesEnded:WithEvent:

从以上链接:

  观察后,交付   将对象触摸到附加的视图,或   否则,他们的性格是   受cancelsTouchesInView的影响,   delayedTouchesBegan,和   delaysTouchesEnded属性。

答案 1 :(得分:2)

以下是我检测双击的解决方案:

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

UITouch *touch = [touches anyObject];

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

}

答案 2 :(得分:1)

在Swift中,您可以避免标准行为,并让UITouch事件传播到视图及其子视图,即使识别出手势,也可以

recognizer.cancelsTouchesInView = false

将调用touchesBegantouchesEnded