Skip to content

UILongPressGestureRecognizer ​

UILongPressGestureRecognizer represents a long-press recognizer: it recognizes the gesture when a finger stays down without moving for a period of time, 0.5 seconds by default.

A long press is a continuous gesture: began fires once the finger has been held long enough, changed fires on every move after that, ended fires when the finger lifts, and cancelled fires when the gesture is interrupted. If the finger moves farther than allowableMovement before the time is up, it doesn't count as a long press and no events fire.

Inheritance: EventTarget → NSObject → UIGestureRecognizer → UILongPressGestureRecognizer

For how to use it, see the guide Responding to gestures.

Constructor ​

UILongPressGestureRecognizer()

Creates a recognizer for a single finger held down for 0.5 seconds.

Instance properties ​

It also inherits the properties of UIGestureRecognizer; the commonly used ones are isEnabled, view, and cancelsTouchesInView. All of its methods are inherited from it too: addEventListener(), removeEventListener(), and requireGestureRecognizerToFail().

PropertyDescription
minimumPressDurationHow long the finger must be held, in seconds
allowableMovementHow far the finger can move before the gesture is recognized, in pt
numberOfTapsRequiredHow many full taps must come before the press
numberOfTouchesRequiredHow many fingers must be held down together

Events ​

The events come from UIGestureRecognizer; listen with addEventListener(). This recognizer fires:

EventWhen it fires
beganWhen the finger has been held long enough
changedWhen the finger moves after the gesture is recognized
endedWhen the finger lifts after the gesture is recognized
cancelledWhen the gesture is interrupted, for example when the app moves to the background

Examples ​

js
const card = new UIView();
const press = new UILongPressGestureRecognizer();
press.addEventListener('began', () => {
  console.log('Pressed');
});
press.addEventListener('ended', () => {
  console.log('Released');
});
card.addGestureRecognizer(press);

Effect: Hold card for 0.5 seconds and the console prints Pressed; when the finger lifts, it prints Released.

Corresponding iOS API ​

ObjCJS
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)]new UILongPressGestureRecognizer(), then addEventListener('began', onPress)
press.minimumPressDuration = 1.0press.minimumPressDuration = 1
press.allowableMovement = 20press.allowableMovement = 20

See also ​