What is the time limit for two taps to be considered a double-tap, on the iPhone OS?
// Edit: Why is this important?
In order to handle single-tap and double-tap
I don't believe there is a default time-limit - it's whatever "feels" right when you try it out. Apple has an example here, where they don't specify a specific time either.
On Developer Forums on developer.apple.com, an Apple developer said:
I have submitted this to Apple as bug 68405.
You can detect any number of tap by the tap gesture. No need of playing with NSTouches
either. For all the user seeking for the solution here it is.
These simple lines of code does the duty of single and double tap functionality.
UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(doubleTapped:)];
doubleTapRecg.delegate = self;
doubleTapRecg.numberOfTapsRequired = 2;
doubleTapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:doubleTapRecg];
UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapped:)];
tapRecg.delegate = self;
tapRecg.numberOfTapsRequired = 1;
tapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:tapRecg];
[tapRecg requireGestureRecognizerToFail:doubleTapRecg];
[doubleTapRecg release];
[tapRecg release];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
switch (tapCount) {
case 1:
[self performSelector:@selector(singleTapMethod) withObject:nil afterDelay:.4];
break;
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapMethod) object:nil];
[self performSelector:@selector(doubleTapMethod) withObject:nil afterDelay:.4];
break;
case 3:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doubleTapMethod) object:nil];
[self performSelector:@selector(tripleTapMethod) withObject:nil afterDelay:.4];
break;
case 4:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tripleTapMethod) object:nil];
[self quadrupleTap];
break;
default:
break;
}
}
@end
for me this code is working prefectly fine.... :) the last post was around august 15 09.... but i was going through the same problem so thought to share the solution i found....
I am not sure if this is the correct way of doing things but this code is working well for me.
in my .h file:
@interface MyViewController : UIViewController {
int tapCount;
}
@property (nonatomic, assign)int tapCount;
@end
in my .m file:
@synthesize tapCount;
- (void)tapGesture:(UIGestureRecognizer*)gesture {
tapCount = tapCount + 1;
[self performSelector:@selector(correctTapCount) withObject:nil afterDelay:0.3];
}
-(void)correctTapCount {
if (tapCount == 1) {
NSLog(@"Single Tap");
}
if (tapCount == 2) {
NSLog(@"Double Tap");
}
//reset TapCount
tapCount = 0;
}
And this is where I add my tap listener (it is on a UIImageView )
//add tap listener
carImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
[carImage addGestureRecognizer:tap];
[tap release];
Good Luck and Happy Coding!
In the Apple provided example of the iPhone Application Programming Guide: Event Handling (mentioned by user Italy) a delay of 0.3
is used:
[self performSelector:@selector(handleSingleTap:) withObject:touchLoc afterDelay:0.3];