Page 145 iPhone Programming Guide - This should the first pdf people read after learning Objective-C
HandlingSingleandMultipleTapGestures
AverycommongestureiniPhoneapplicationsisthetap:the usertapsanobjectwithhisorherfinger.
Aresponderobjectcanrespondtoasingletapinoneway,ado uble-tapinanother,andpossiblya
triple-tapinyetanotherway.Todeterminethenumberoftimestheu sertappedaresponderobject,
yougetthevalueofthetapCountpropertyofaUITouchobjec t.
ThebestplacestofindthisvaluearethemethodstouchesBe gan:withEvent:and
touchesEnded:withEvent:.Inmanycases,thelattermetho dispreferredbecauseitcorrespondsto
thetouchphaseinwhichtheuserliftsafingerfromatap.By lookingforthetapcountinthetouch-up
phase(UITouchPhaseEnded),youensurethatthefingerisr eallytappingandnot,forinstance,touching
downandthendragging.
144 HandlingMulti-TouchEvents
2008-07-08 | ©2008AppleInc. AllRightsReserved.
In Listing 7-1, the touchesEnded:withEvent: method implementation responds to a double-tap
gesture by zooming in on (or out from) the content shown in a scroll view.
Listing 7-1 Handling a double-tap gesture
- (void) touchesEnded

NSSet*)touches withEvent

UIEvent*)event
{
UIScrollView *scrollView = (UIScrollView*)[self superview];
UITouch *touch = [touches anyObject];
CGSize size;
CGPoint point;
if([touch tapCount] == 2) {
if(![_viewController _isZoomed]) {
point = [touch locationInView:self];
size = [self bounds].size;
point.x /= size.width;
point.y /= size.height;
[_viewController _setZoomed:YES];
size = [scrollView contentSize];
point.x *= size.width;
point.y *= size.height;
size = [scrollView bounds].size;
point.x -= size.width / 2;
point.y -= size.height / 2;
[scrollView setContentOffset

oint animated:NO];
}
else
[_viewController _setZoomed:NO];
}
}
A complication arises when a responder object wants to handle a single-tap and a double-tap gesture
in different ways. For example, a single tap might select the object and a double tap might display a
view for editing the item that was double-tapped. How is the responder object to know that a single
tap is not the first part of a double tap? Here is how a responder object could handle this situation
using the event-handling methods just described:
1. In touchesEnded:withEvent:, when the tap count is one, the responder object sends itself a
performSelector:withObject:afterDelay: message. The selector identifies another method
implemented by the responder to handle the single-tap gesture; the object for the second parameter
is the related UITouch object; the delay is some reasonable interval between a single- and a
double-tap gesture.
2. In touchesBegan:withEvent:, if the tap count is two, the responder object cancels the pending
delayed-perform invocation by sending itself a cancelPreviousPerformRequestsWithTarget:
message. If the tap count is not two, the method identified by the selector in the previous step
for single-tap gestures is invoked after the delay.
3. In touchesEnded:withEvent:, if the tap count is two, the responder performs the actions necessary
for handling double-tap gestures.
C H A P T E R 7
Event Handling