Use brief taps on the screen to implement button-like interactions with your content.
Tap gestures detect one or more fingers touching the screen briefly. The fingers involved in these gestures must not move significantly from the initial touch points, and you can configure the number of times the fingers must touch the screen. For example, you might configure tap gesture recognizers to detect single taps, double taps, or triple taps.
You can attach a gesture recognizer in one of these ways:
Programmatically. Call the
addmethod of your view.
In Interface Builder. Drag the appropriate object from the library and drop it onto your view.
UITap object provides event handling capabilities similar to those of a button—it detects a tap in its view and reports that tap to your action method. Tap gestures are discrete, so your action method is called only when the tap gesture is recognized successfully. You can configure a tap gesture recognizer to require any number of taps—for example, single taps or double taps—before your action method is called.
Listing 1 shows an action method that responds to a successful tap in a view by animating that view to a new location. Always check the gesture recognizer’s
state property before taking any actions, even for a discrete gesture recognizer.
If the code for your tap gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:
isproperty of the view is set to
User Interaction Enabled
true. Image views and labels set this property to
The number of taps was equal to the number specified in the
Of Taps Required
The number of fingers was equal to the number specified in the
Of Touches Required