Gesture Recognizers

There are two ways to handle touches on a view.

  1. By responding to raw touch events (touch down, touch moved, touch up).
    This is similar to using the mousePressed(), mouseDragged(), mouseReleased() functions in Processing.
    It is also how we’ve been interfacing with UIButton objects.
  2. By registering actions on certain predefined gestures (tap, swipe, pan, pinch, rotation, long press).
    This is done using subclasses of UIGestureRecognizer.

The UIGestureRecognizer Class

UIGestureRecognizer is abstract, meaning that we can’t ever use it directly. We need to use one of its concrete subclasses:

  • UITapGestureRecognizer listens for single or multiple taps, involving one or many touches.
  • UIPinchGestureRecognizer listens for pinching gestures, involving two touches (moving two fingers towards or away from each other).
  • UIRotationGestureRecognizer listens for rotation gestures, involving two touches (moving two fingers opposite from each other in a circular motion).
  • UISwipeGestureRecognizer listens for swiping gestures, involving one or many touches.
  • UIPanGestureRecognizer listens for panning gestures, involving one or many touches.
    The difference with UISwipeGestureRecognizer is that the panning gesture is continuous whereas the swiping gesture is triggered after the fact. For example, you would listen for a swipe to change pages in an eBook, but listen for a pan to move pieces around in a puzzle game.
  • UILongPressGestureRecognizer listens for long press gestures, involving one or many touches. The touch must stay down for a determined period of time and must not move more than the allowable distance.

To use a UIGestureRecognizer, it must be added to the UIView it listens on and it must be registered with a callback method to handle the gesture when it happens. This can be done in the Interface Builder or preferably in code.

In order to properly handle an incoming gesture, you must check the state property of UIGestureRecognizer.

Each concrete gesture recognizer also provides methods and properties to help you handle it properly.


  1. Gesture Recognizers. Event Handling Guide for iOS. Apple, Mar 10 2011.

Comments are closed.