Reputation: 199
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *aTouch in touches) {
if (aTouch.tapCount >= 2) {
// The view responds to the tap
}
}
}
I'm using the code above to detect double tap gesture; however, how can I set the code to happen only once?
In other words, when you tap once, the character jumps. When you tap twice in quick succession, the character will double jump. But how do you set the taps in a way that the character will not continuously double jump and go higher off the single-view without initially taping once?
Upvotes: 2
Views: 256
Reputation: 12214
A very simple approach of achieving this is by declaring a global bool
variable and set its value once the double tap has been detected!
Something like this:
@interface MyViewController()
{
bool isTapped;
}
@end
@implementation MyViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *aTouch in touches) {
if (aTouch.tapCount >= 2) {
if(!isTapped) {
// The view responds to the tap
isTapped = YES;
}
}
}
}
@end
Hope this helps
Upvotes: 1
Reputation: 15015
Not sure if this helps and also just take one flag and set it yes or no accordingly:-
UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.delegate = self;
tapRecognizer.minimumPressDuration = //Up to you;
[self.someView addGestureRecognizer:tapRecognizer];
Upvotes: 0