Home > DeveloperSection > Articles > Tap Gesture Recognizer in iOS

Tap Gesture Recognizer in iOS


iPhone iPhone  Ios  Objective C 
Ratings:
0 Comment(s)
 533  View(s)
Rate this:

Tap Gesture Recognizer in iOS


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)

 


Don't want to miss updates? Please click the below button!

Follow MindStick