Search
Recommended for You

Recovering multiple touches


A view that handles multiple touch points must announce itself. Add the isMultipleToucheEnabled handler and have it return YES. Doing so allows your touch methods (touchesBegan, touchesMoved, touchesEnded) to work with several touchpoints at once.

- (BOOL) isMultipleTouchEnabled {return YES;}

Touches are are passed to these methods as an unordered set (NSSet *). You can recover an array of the touches and count them.

NSArray *allTouches = [touches allObjects];
count = [allTouches count];

You'll receive one touch for each continuous point of contact on the screen, up to five touches total. Even though the set of touches is not ordered, each touch maintains a relative position to each other in memory. Apple provided this beautiful sample code that examines the address for each touch and allows you to sort and identify those touches. Here's how the touches are sorted:

NSArray *sortedTouches = [[touches allObjects] sortedArrayUsingSelector:@selector(compareAddress:)];

And here's the comparison that leverages their addresses to determine the touch order:

 @implementation UITouch (TouchSorting)
- (NSComparisonResult)compareAddress:(id)obj {
    if ((void *)self < (void *)obj) return NSOrderedAscending;
    else if ((void *)self == (void *)obj) return NSOrderedSame;
	else return NSOrderedDescending;
}
@end

Ordering your touches lets you identify the series of movements based on each finger. You can track those movements individually and create a trace for each touch point. You can also use sorted touches to draw meaning from those movements. For example, you can detect whether the user is pinching, dragging or even rotating.

Although the iPhone permits five touches at a time, as a rule, you probably want to limit your multi-touch interactions to two fingers. With more than two fingers, the interaction becomes pretty complicated and muddy. Once again, sorting lets you pick just the first two touch points to track and consistently returns those points to you over the lifetime of the gesture.

AddThis Social Bookmark Button
Comments (0)

Leave a comment


Type the characters you see in the picture above.