UISwipeGestureRecognizer
UISwipeGestureRecognizer represents a swipe recognizer: it recognizes the gesture when a finger moves quickly across the screen in a given direction. By default it recognizes a one-finger swipe to the right. Moving slowly doesn't count as a swipe.
A swipe is a discrete gesture: once recognized, it's over, and ended fires exactly once. The event doesn't say which direction was recognized; to tell directions apart, create one recognizer per direction.
Swiping hasn't been checked with real touches yet. The gesture itself is not verified in the simulator.
Inheritance: EventTarget → NSObject → UIGestureRecognizer → UISwipeGestureRecognizer
For how to use it, see the guide Responding to gestures.
Constructor
Creates a recognizer for one-finger swipes to the right.
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 |
|---|---|
numberOfTouchesRequired | How many fingers must swipe together |
direction | Which directions of swipe to recognize |
Events
The events come from UIGestureRecognizer; listen with addEventListener(). This recognizer fires:
| Event | When it fires |
|---|---|
ended | When the swipe is recognized. A swipe fires only this one event |
Examples
const card = new UIView();
const swipeLeft = new UISwipeGestureRecognizer();
swipeLeft.direction = ['left'];
swipeLeft.addEventListener('ended', () => {
console.log('Swiped left');
});
card.addGestureRecognizer(swipeLeft);Effect: When a finger moves quickly to the left across card, it prints Swiped left. Not verified in the simulator.
Corresponding iOS API
| ObjC | JS |
|---|---|
swipe.direction = UISwipeGestureRecognizerDirectionLeft | swipe.direction = ['left'] |
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | swipe.direction = ['left', 'right'] |
swipe.numberOfTouchesRequired = 2 | swipe.numberOfTouchesRequired = 2 |
See also
- Responding to gestures
UIGestureRecognizer: superclass, with the events and shared properties