Am I supposed to be able to detect multi-finger taps this way?
-
21-09-2019 - |
Question
This is the touchesBegan method for a view that has multiple touches enabled.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
if ([touches count] > 1)
NSLog(@"multi touches: %d fingers", [touches count]);
NSUInteger numTaps = [touch tapCount];
if (numTaps == 1) {
NSLog(@"single tap");
} else {
NSLog(@"multi tap: %d", numTaps);
}
}
I never seem to log a multi-touch. Just single and double taps. Am I wrong to have assumed it was as easy as getting the count for touches?
Solution 2
I tried three different ways and only one can return two to five finger taps. The winning mechanism is NSSet *touch = [event allTouches];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
// the next line will not ever return a multiple tap
if ([touches count] > 1)
NSLog(@"multi-touches %d", [touches count]);
// the next line will return up to 5 (verified) simultaneous taps (maybe more)
NSSet *touch2 = [event allTouches];
if ([touch2 count] > 1)
NSLog(@"multi-touches2 %d", [touch2 count]);
// the next line only returns 1 tap
NSSet *touch3 = [event touchesForView:self];
if ([touch3 count] > 1)
NSLog(@"multi-touches2 %d", [touch3 count]);
}
OTHER TIPS
You should set multipleTouchEnabled
property to YES
on the view to let it send you multiple number of UITouch
objects.
Beside that, only UITouch
objects that changed are passed. If you touch some location and don't move your finger and after that, touch another location, only the new touch object will be passed. You should query UIEvent
object for all active touches in the view:
[event touchesForView:self]
Winning mechanism? With errors and vars that aren't even being used?
incompatible Objective-C types 'struct AViewController *', expected 'struct UIView *' when passing argument 1 of 'touchesForView:' from distinct Objective-C type
AViewController.m:64:unused variable 'touch'