How do I detect two finger开发者_运维问答s tap on the iPhone?
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
If you're not targeting 3.2+:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 2) {
//etc
}
}
Set the multiTouchEnabled
property to YES
.
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.
精彩评论