articles

Home / DeveloperSection / Articles / Tap Gesture Recognizer in iOS

Tap Gesture Recognizer in iOS

Tarun Kumar2893 29-Nov-2015

Whenever we are creating an app for iPhone/iPad touch devices we always use tap gestures to recognize or taking some actions. In iOS tap gestures are registered with the UITapGestureRecognizer class. Here we will show you how to use this class to recognize single and multiple tap gestures.

Here we are providing some points to remember:

1. First of all we will enable user interaction on the UI element that's going to receive the taps. We can do this by setting the UserInteractionEnabled property to true like that: 

imageView.UserInteractionEnabled = true;

We can also do it inside the ViewDidLoad method of the ViewController or any

other method that we are using to manipulate our apps UI elements.

 

2. After that, we will create a new UITapGestureRecognizer and passing action

name which will handle the tap gesture like this: 

UITapGestureRecognizer tapGesture = new UITapGestureRecognizer (TapedImage);

 

3. After that we will create an action which handles the tap gestures. Here we are

created a method called TapedImage to rotate clockwise view(90 degrees) and

show an alert view: 

tap.View.Transform *= CGAffineTransform.MakeRotation ((float)Math.PI / 2);
tapped = true;
alert = UIAlertController.Create ("Image is Tapped", UIAlertControllerStyle.Alert);
alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
ViewController (alert, true, null);

 

4. Now, at last we will add the gesture recognizer in our UI element, like this:

imageView.AddGestureRecognizer (tapGesture);

 

We can also count the number of taps using gesture recognizer, it will responds by

using NumberOfTapsRequired property of the tap gesture recognizer. 


For Example:

numberOfGesture.NumberOfTapsRequired = 2;

(by default number of taps required is 1)

 


Updated 18-Jul-2019

Leave Comment

Comments

Liked By