UITapGetureRecognizer in IOS

UITapGesturRecognizer, what is that. You touch screen of your smartphones, in technical terms it is known as gesture. There are different types of gesture API’s available to IOS developers


In this post, we will cover UITapGestureRecognizer and learn how to use it in our iOS app.Apart from UITapGestureRecognizer we have UIPanGestureRecognizer and UIrotationGestureRecognizer. Let us briefly understand when to use these API’s and in which cases they are needed.

UITapGetureRecognizer: UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for single or multiple taps. For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.

UIPanGestureRecognizer: UIPanGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for panning (dragging) gestures. The user must be pressing one or more fingers on a view while they pan it. Clients implementing the action method for this gesture recognizer can ask it for the current translation and velocity of the gesture.

UIRotationGestureRecognizer: UIRotationGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for rotation gestures involving two touches. When the user moves the fingers opposite each other in a circular motion, the underlying view should rotate in a corresponding direction and speed.

Now all three gestures are cleared to you and its time to know how we can use them programmatically.


We can use UITapGetureReconizer API to detect when user taps on screen with his finger. For this, we simply add our gestureRecognizer object to a view if we want to detect tap on whole view or screen. After that, we will add it to self.view or we will add it to particular view using method addGestureRecognizer

Let us create a project named HowToUseTapGesture, Open your ViewController.m file and in your ViewDidLoad method add below code

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAtPoint:)];
[self.view addGestureRecognizer:tapGesture];

 [tapGesture release];
tapGesture = nil

In above code, we created an object of UITapGestureReconizer. Allocate it with a target and gave him selector which will be gets called when user tap on screen of the device. Then we add newly created gesture to our main view becuase we want to detect tap on whole screen. We have to give number of taps a user should do on screen, in order to trigger action for the geature. We want to call selector associated with gesture when user tap only 1 time that’s why we assigned value 1 to property named as numberOfTapsRequired. So, now our selector will gets called when user tap or touch the device screen. Lastly we release our gesture object and set it to nil. If using ARC then last two lines of code are not required.

Finally we implement our selector as shown below

-(void)tapAtPoint:(UITapGestureRecognizer *)tapgesture

    NSLog(@”Tap detected”);

Imaplementing Gesture selector

Run your project and see in console when you tap your simulator screen or device screen.


Source Code: Download above source code from here