Skip to content

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 ​

UITapGestureRecognizer()

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().

PropertyDescription
numberOfTapsRequiredHow many taps are needed. 2 is a double tap
numberOfTouchesRequiredHow many fingers must tap together

Events ​

Events come from UIGestureRecognizer; listen with addEventListener(). It fires:

EventWhen it fires
endedWhen the tap is recognized. A tap fires only this event

Examples ​

js
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 ​

ObjCJS
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)]new UITapGestureRecognizer(), then addEventListener('ended', onTap)
tap.numberOfTapsRequired = 2tap.numberOfTapsRequired = 2
tap.numberOfTouchesRequired = 2tap.numberOfTouchesRequired = 2

See also ​