Alexis King
Alexis King

Reputation: 43862

UITapGestureRecognizer Stops touchesEnded

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];

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?

Upvotes: 1

Views: 3194

Answers (3)

Domenico Casillo
Domenico Casillo

Reputation: 86

In Swift, you can avoid the standard behaviour and let the UITouch event propagate to view and its child, even if a gesture is recognized, with

recognizer.cancelsTouchesInView = false

Both touchesBegan and touchesEnded will be called.

Upvotes: 3

Rafał Sroka
Rafał Sroka

Reputation: 40028

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   
}

}

Upvotes: 2

Kris Markel
Kris Markel

Reputation: 12112

Adding a gesture recognizer to a view changes the behavior of several touch handling methods, including touchesEnded:WithEvent:.

From the above link:

After observation, the delivery of touch objects to the attached view, or their disposition otherwise, is affected by the cancelsTouchesInView, delaysTouchesBegan, and delaysTouchesEnded properties.

Upvotes: 2

Related Questions