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.

// -----------------------------
// One finger, two taps
// -----------------------------
// Create gesture recognizer
UITapGestureRecognizer *oneFingerTwoTaps = 
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
// Add the gesture to the view
[[self view] addGestureRecognizer:oneFingerTwoTaps];

Below is a recognizer for two fingers, two taps:

// -----------------------------
// Two fingers, two taps
// -----------------------------
UITapGestureRecognizer *twoFingersTwoTaps = 
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
[twoFingersTwoTaps setNumberOfTapsRequired:2];
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:twoFingersTwoTaps];

The following gestures recognize swipes up and down:

// -----------------------------
// One finger, swipe up
// -----------------------------
UISwipeGestureRecognizer *oneFingerSwipeUp = 
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[[self view] addGestureRecognizer:oneFingerSwipeUp];
// -----------------------------
// One finger, swipe down
// -----------------------------
UISwipeGestureRecognizer *oneFingerSwipeDown = 
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[[self view] addGestureRecognizer:oneFingerSwipeDown];

Two recognize two fingers rotating on a view:

// -----------------------------
// Two finger rotate  
// -----------------------------
UIRotationGestureRecognizer *twoFingersRotate = 
  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
[[self view] addGestureRecognizer:twoFingersRotate];

And finally, two finger pinch:

// -----------------------------
// Two finger pinch
// -----------------------------
UIPinchGestureRecognizer *twoFingerPinch = 
  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];

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.

* One finger, two taps 
- (void)oneFingerTwoTaps
  NSLog(@"Action: One finger, two taps");
* Two fingers, two taps
- (void)twoFingersTwoTaps {
  NSLog(@"Action: Two fingers, two taps");
* One finger, swipe up
- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer 
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
* One finger, swipe down
- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer 
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
* Two finger rotate   
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 
  // Convert the radian value to show the degree of rotation
  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
* Two finger pinch
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
  NSLog(@"Pinch scale: %f", recognizer.scale);
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?

  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?


  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 .


  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.