Class UITapGestureRecognizer

Class UITapGestureRecognizer is a subclass of UIGestureRecognizer.

Source code in Tap.zip

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

Recognizing single and double taps

The single tap recognizer must not be allowed to recognize a single tap until the double tap recognizer has told it that the tap is not the first half of a double tap. See requireGestureRecognizerToFail:.

If another tap (single or double) is recognized while a 1 or 2 is on the screen, we cancel the previous request for a call to wearOff and schedule another call to wearOff. See cancelPreviousPerformRequestsWithTarget:selector:object:.

I was surprised that I was allowed to name one of the methods double: (with a colon), since double is a keyword in the language Objective-C.

Things to try

  1. Replace single: and double: by the following method. It does the work of either one. On a 64-bit device (e.g., iPhone Retina (4-inch 64-bit)), an NSUInteger must be formatted with %lu.
    - (void) handle: (UITapGestureRecognizer *) recognizer {
    	[NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(wearOff) object: nil];
    	label.text = [NSString stringWithFormat: @"%lu", recognizer.numberOfTapsRequired];
    	[self performSelector: @selector(wearOff) withObject: nil afterDelay: delay];
    }
    

  2. Detect a triple tap.