Tap Count

Source code in Tap.zip

  1. main.m
  2. Class TapAppDelegate
  3. Class View

The methods singleTap and doubleTap of class View are called in touchesEnded:withEvent: at the end of a single or double tap. After a two-second delay, the method noTap is called to reset the display. You can’t name a method double because double is a keyword in this language.

performSelector:withObject:afterDelay: and cancelPreviousPerformRequestsWithTarget:

touchesEnded:withEvent: can’t call singleTap as soon as it has detected a single tap. After all, the single tap might turn out to be the first tap of a double tap. We therefore request that a call be made to singleTap after a delay of 0.3 seconds. If a second tap arrives, we cancel this request. See Handling Tap Gestures.

The method performSelector:withObject:afterDelay: lets you call the method of your choice, belonging to the object of your choice, after the delay (in seconds) of your choice.

For another way to distingush between single and double taps, see here.

Things to try

  1. Detect a triple tap.

  2. Use a UILabel instead of a subclass of UIView. See Gone. You will have to set the userInteractionEnabled property of the UILabel to YES.