Gestures Recognizers – Tap, Pinch/Zoom, Rotate, Swipe, Pan, Long Press

The UIGestureRecognizer class is available to help with detecting and responding to the various UI gestures common on iOS devices. UIGestureRecognizer is an abstract class, with the following concrete subclasses, one for each type of available recognizer:

  1. UITapGestureRecognizer
  2. UIPinchGestureRecognizer
  3. UIRotationGestureRecognizer
  4. UISwipeGestureRecognizer
  5. UIPanGestureRecognizer
  6. UILongPressGestureRecognizer


Depending on the type of recognizer, there are various behaviors that you can configure. For instance, with the UITapGestureRecognizer, you can specify the number of taps and number of touches. As an example, you could specify that two fingers are required, tapping twice with both fingers.

In response to recognized gestures, an action messages is sent to a target object that you specify. Depending on the type of gesture additional information about the gesture may be available in the action method, for example, the start location of a swipe, or the scale factor of a pinch/zoom.

Once you’ve defined a gesture, you add the recognizer to the view. From there, the view is responsible for recognizing when a gesture has occurrred and calling the relevant action.

Gesture Recognizer Examples

Let’s walk through configuration of a few gestures to see how all this works.

Below I’ve defined a recognizer for managing taps, notice how I’ve specified that there are two taps required, with just one touch (finger). The @selector specifies the method that will be called when the gesture is recognized.

Below is a recognizer for two fingers, two taps:

The following gestures recognize swipes up and down:

Two recognize two fingers rotating on a view:

And finally, two finger pinch:

There are two additional gestures not shown above that you can also tinker with, UIPanGestureRecognizer and UILongPressGestureRecognizer.

Gesture Recognizer Action Methods

For each of the above definitions, below are the associated action methods.

Source Code

To get a feel for how gestures and the corresponding action methods work, download the Xcode project below and install the application on a device from within Xcode – look at the console output as you perform the various gestures to see the log messages for each gesture as it’s recognized.

Gesture Recognizer Xcode Project

  1. Hi,
    Thanks for the post.

    How does one detect and object that was touched and dragged using the Gestures Recognizers?
    Thanks,
    Jason

  2. I have an app that detects a pinch (UIPinchGestureRecognize is used). It runs and works in the simulator for 5.0 and 5.1 with Xcode 4.3, but when I upgraded to 4.3.2 it still runs in both 5.0 and 5.1 but it doesn’t work. Doesn’t detect the pinch. I found this article,downloaded your code and the same thing happens. It detects the swipes up and down and the taps, but it doesn’t register the Pinches. By chance have you (or has anyone) tested this in Xcode 4.3.2?

    Thanks!!!

  3. Wow, all I can say is wow, thanks, this is what I’ve been trying to work out amoung other things. Fantastic.

  4. hi,

    I am new in ios development . Has any option to test UIPinchGestureRecognizer
    UIRotationGestureRecognizer in ios simulator?
    I am using xcode 4.5 and ios simulator 6,5,5.1 .

    Thanks

  5. how would you do a gesture for drawing something or in my case the reverse erase for my drawing game i want to be able to erase (with a finger) like rub out the mistake.

    Thanks in advance

    • Although I have not written anything like that, I imagine you would need to track movements manually and make the necessary UI updates.

Comments are closed.