Paulo Ferreira
Paulo Ferreira

Reputation: 421

How do I detect two fingers tap on the iPhone?

How do I detect two fingers tap on the iPhone?

Upvotes: 6

Views: 6832

Answers (4)

Dave DeLong
Dave DeLong

Reputation: 243146

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

Upvotes: 12

shosti
shosti

Reputation: 7422

If you're not targeting 3.2+:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 2) {
        //etc
    }
}

Upvotes: 4

drawnonward
drawnonward

Reputation: 53669

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.

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

Set the multiTouchEnabled property to YES.

Upvotes: 2

Related Questions