views:

302

answers:

2

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?

+1  A: 

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]
Mehrdad Afshari
I just added the fact that the mutliple touch property is enabled (and it doesn't work).
mahboudz
That doesn't work. This does: NSSet *touch = [event allTouches];
mahboudz
mahboudz: It should work if both touches start in the specific view. Of course, by `self`, I mean the view instance.
Mehrdad Afshari
A: 

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]);
}
mahboudz