6

How do I detect two fingers tap on the iPhone?

Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
Paulo Ferreira
  • 423
  • 2
  • 8
  • 16

4 Answers4

12

If you can target OS 3.2 or above, you can use a UITapGestureRecognizer. It's really easy to use: just configure it and attach it to the view. When the gesture is performed, it'll fire the action of the gestureRecognizer's target.

Example:

UITapGestureRecognizer * r = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewWasDoubleTapped:)];
[r setNumberOfTapsRequired:2];
[[self view] addGestureRecognizer:r];
[r release];

Then you just implement a - (void) viewWasDoubleTapped:(id)sender method, and that will get invoked when [self view] gets double-tapped.

EDIT

I just realized you might be talking about detecting a single tap with two fingers. If that's the case, the you can do

[r setNumberOfTouchesRequired:2]
.

The primary advantage of this approach is that you don't have to make a custom view subclass

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
4

If you're not targeting 3.2+:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 2) {
        //etc
    }
}
shosti
  • 7,332
  • 4
  • 37
  • 42
2

Set the multiTouchEnabled property to YES.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0

If your requirements allow, use UITapGestureRecognizer. Otherwise, implement the following UIResponder methods in your custom UIView:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

Track throughout to see how many touches there were and whether or not they moved more than your tap/drag threshold. You must implement all four methods.

drawnonward
  • 53,459
  • 16
  • 107
  • 112