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().
| Property | Description |
|---|---|
minimumPressDuration | How long the finger must be held, in seconds |
allowableMovement | How far the finger can move before the gesture is recognized, in pt |
numberOfTapsRequired | How many full taps must come before the press |
numberOfTouchesRequired | How many fingers must be held down together |
Events
The events come from UIGestureRecognizer; listen with addEventListener(). This recognizer fires:
| Event | When it fires |
|---|---|
began | When the finger has been held long enough |
changed | When the finger moves after the gesture is recognized |
ended | When the finger lifts after the gesture is recognized |
cancelled | When the gesture is interrupted, for example when the app moves to the background |
Examples
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
| ObjC | JS |
|---|---|
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)] | new UILongPressGestureRecognizer(), then addEventListener('began', onPress) |
press.minimumPressDuration = 1.0 | press.minimumPressDuration = 1 |
press.allowableMovement = 20 | press.allowableMovement = 20 |
See also
- Responding to gestures: changing how long a long press takes, and following the finger after a long press
UIGestureRecognizer: superclass, with the events and shared properties