UITapGestureRecognizer
UITapGestureRecognizer represents a tap recognizer: it recognizes the gesture when a finger goes down and lifts quickly. How many taps and how many fingers can both be set; the default is a single tap with one finger.
A tap is a discrete gesture: once recognized it is over, and ended fires only once.
Inheritance: EventTarget → NSObject → UIGestureRecognizer → UITapGestureRecognizer
For how to use it, see the guide Responding to gestures.
Constructor
Creates a recognizer for a single tap with one finger.
Instance properties
Also inherits the properties of UIGestureRecognizer; the commonly used ones are isEnabled, view, and cancelsTouchesInView. Its methods are all inherited from it too: addEventListener(), removeEventListener(), and requireGestureRecognizerToFail().
| Property | Description |
|---|---|
numberOfTapsRequired | How many taps are needed. 2 is a double tap |
numberOfTouchesRequired | How many fingers must tap together |
Events
Events come from UIGestureRecognizer; listen with addEventListener(). It fires:
| Event | When it fires |
|---|---|
ended | When the tap is recognized. A tap fires only this event |
Examples
const card = new UIView();
const tap = new UITapGestureRecognizer();
tap.addEventListener('ended', (e) => {
const { location } = /** @type {UIGestureRecognizerEvent} */ (e);
if (location !== null) {
console.log('Tapped at', location.x, location.y);
}
});
card.addGestureRecognizer(tap);Effect: Tapping card prints the coordinates of the tap within card to the console.
Corresponding iOS API
| ObjC | JS |
|---|---|
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)] | new UITapGestureRecognizer(), then addEventListener('ended', onTap) |
tap.numberOfTapsRequired = 2 | tap.numberOfTapsRequired = 2 |
tap.numberOfTouchesRequired = 2 | tap.numberOfTouchesRequired = 2 |
See also
- Responding to gestures: getting the tap location, responding to double taps, making a single tap wait for a double tap to fail
UIGestureRecognizer: the superclass, its events and shared properties