I need some help with the touchesEnded function. I want to start a NSTimer when there are no fingers on the screen using the touchesEnded function. Is this possible?. Currently I have a touchesBegan function working :-).
Here’s my code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps >= 2)
{
self.label.text = [NSString stringWithFormat:@"%d", numTaps];
self.label2.text = @"+1";
[self.button setHidden:YES];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)showButton
{
[self.button setHidden:NO];
}
It’s tricky. You don’t get an event that tells you that all fingers are up; each
touchesEndedonly tells you that this finger is up. So you have to keep track of touches yourself. The usual technique is to store touches in a mutable set as they arrive. But you can’t store the touches themselves, so you have to store an identifier instead.Start by putting a category on UITouch, to derive this unique identifier:
Now we must maintain our mutable set throughout the period while we have touches, creating it on our first touch and destroying it when our last touch is gone. I’m presuming you have an NSMutableSet instance variable / property. Here is pseudo-code: