Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

id title brief sdk
Use a Tap Gesture
In iOS, tap gestures are registered with the UITapGestureRecognizer class. This recipe demonstrates how to use this class to recognize single and multiple tap gestures.


Tap Gesture Tap Gesture

    <li><p>First, let's enable user interaction on the UI element that's going to receive the taps. We can do that by setting the <code>UserInteractionEnabled</code> property to true:</p>
    	<pre><code>imageView.UserInteractionEnabled = true;</code></pre>
    	<p>We can do this inside the <code>ViewDidLoad</code> method of the <code>ViewController</code>, or whatever method you are using to manipulate the UI element.</p>
    <li><p>Next we'll create a new <code>UITapGestureRecognizer</code>, passing in the name of the Action that will handle the tap gesture:</p>
    	<pre><code>UITapGestureRecognizer tapGesture = new UITapGestureRecognizer (TapThat);</code></pre>
    <li><p>Next we'll create the Action to handle the tap gesture. In our example, we're using a method called <code>TapThat</code> to rotate the View 90 degrees clockwise and show an alert view:</p>
    	<pre><code>tap.View.Transform *= CGAffineTransform.MakeRotation ((float)Math.PI / 2);
    			tapped = true;
    			alert = UIAlertController.Create ("Card Tapped", "This card has been tapped", UIAlertControllerStyle.Alert);
    			alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
    			PresentViewController (alert, true, null);</code></pre>
    <li><p>Finally, we can add the gesture recognizer to our UI element:</p>
    	<pre><code>imageView.AddGestureRecognizer (tapGesture);</code></pre>

Additional Information

We can control the number of taps our gesture recognizer responds to by editing the NumberOfTapsRequired property of the tap gesture recognizer. For example, we could change the code above to respond to two taps with the following code:

tapGesture.NumberOfTapsRequired = 2;

The default number of taps required is 1.

You can’t perform that action at this time.