views:

62

answers:

2

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?

A: 

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.

Robot K
A: 

Here is my solution fordetecting double-taps:

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

UITouch *touch = [touches anyObject];

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

}
reecon
Thanks! Worked perfectly. I didn't realize that you could detect double-taps without a gesture recognizer. Silly me!
Jake King