Single, Double and Triple Taps

If you need to tinker with the threshold (time between clicks) when working with single, double and triple taps on the touch screen, one approach for this follows. What follows is a short example that demonstrates how you can manage the delay between taps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-(void)oneTap
{
  NSLog(@"Single tap");
}
 
-(void)twoTaps
{
  NSLog(@"Double tap");
}
 
-(void)threeTaps
{
  NSLog(@"Triple tap");
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];
 
  switch ([touch tapCount]) 
  {
    case 1:
      [self performSelector:@selector(oneTap) withObject:nil afterDelay:.5];
      break;
 
    case 2:
      [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(oneTap) object:nil];
      [self performSelector:@selector(twoTaps) withObject:nil afterDelay:.5];
      break;
 
    case 3:
      [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(twoTaps) object:nil];
      [self performSelector:@selector(threeTaps) withObject:nil afterDelay:.5];
      break;
 
    default:
      break;
  }
}

Notice in the case of double and triple taps that the previous request is canceled prior to invoking the selector that matches based on the number of taps.

Although this approach is helpful in many cases, there still is the opportunity for more than one tap to be acknowledged based on the user interaction and your specific needs (e.g. both single and double tap events generated). The solution beyond the above is to roll your own event processing code, which would consist of tracking tap counts along with comparing time stamps between taps.

  1. Awesome. Very clean, totally works. Thanks much.

Comments are closed.