Ilya Suzdalnitski
Ilya Suzdalnitski

Reputation: 53540

Amount of touches

I want to get amount of taps anywhere on the screen.

Here is my code:

-(void) touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
    DebugLog(@"touches: %d", [touches count]);
}

This method is implemented in an active view controller.

But it doesn't detect multiple taps - it reports me them as a single tap.

How can I get amount of taps?

Thanks.

Upvotes: 0

Views: 817

Answers (2)

Mike
Mike

Reputation: 717

Sounds like you may simply have to enable "Multiple Touch" via Interface Builder's View Attributes Inspector on the UI element or view you are working with.

I always forget this and run into the same problem as you, until I remember two hours later that I have to check that box.

Upvotes: 1

Tim
Tim

Reputation: 60110

The issue lies in how touches are reported in that method. The NSSet touches in that method contains one or more instances of UITouch, and each UITouch instance represents touch events from a single finger on the screen. So if the user touches the screen with two fingers, the touches set will contain two UITouch objects.

The issue is that a single UITouch may have multiple taps without being considered multiple UITouches. If the device detects two taps (finger down and up without significant motion) in roughly the same location, it will combine them into a single UITouch containing multiple taps. In this case, you use the tapCount selector on UITouch to figure out how many taps that touch had.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Got %u touches", [touches count]);
    for(UITouch *touch in touches) {
        NSLog(@"Touch had %u taps", [touch tapCount]);
    }
}

For more info:

Upvotes: 3

Related Questions