UIPinchGestureRecognizer
UIPinchGestureRecognizer represents a pinch gesture recognizer: it recognizes two fingers spreading apart or pinching together. Use it for two-finger zoom.
Pinch is a continuous gesture and fires began, changed, ended, and cancelled. The event object carries scale, the distance between the two fingers as a multiple of the distance when the gesture began, and velocity, how fast it changes. scale starts at 1 for every gesture.
Pinching has not yet been checked with real touches; both the gesture itself and the data its events carry are not verified in the simulator.
Inheritance: EventTarget → NSObject → UIGestureRecognizer → UIPinchGestureRecognizer
For how to use it, see the guide Responding to gestures.
Constructor
Creates a recognizer for two-finger pinches.
Instance properties
Also inherits the properties of UIGestureRecognizer; commonly used ones are isEnabled, view, and cancelsTouchesInView. Its methods are all inherited as well: addEventListener(), removeEventListener(), requireGestureRecognizerToFail().
| Property | Description |
|---|---|
scale | The distance between the two fingers as a multiple of the distance when the gesture began |
velocity read-only | How much scale changes per second |
Events
The events come from UIGestureRecognizer; listen with addEventListener(). It fires:
| Event | When it fires |
|---|---|
began | When a pinch is recognized |
changed | Each time the distance between the fingers changes after that |
ended | When the fingers lift |
cancelled | When the gesture is interrupted |
Examples
const card = new UIView();
const pinch = new UIPinchGestureRecognizer();
let baseScale = 1;
pinch.addEventListener('changed', (e) => {
const { scale } = /** @type {UIPinchGestureRecognizerEvent} */ (e);
card.transform = CGAffineTransform.scale(baseScale * scale);
});
pinch.addEventListener('ended', (e) => {
const { scale } = /** @type {UIPinchGestureRecognizerEvent} */ (e);
baseScale = baseScale * scale;
});
card.addGestureRecognizer(pinch);Effect: spreading two fingers on card makes it larger; pinching makes it smaller. It keeps its size after the fingers lift, and the next pinch continues from the current size. Not verified in the simulator.
Corresponding iOS API
| ObjC | JS |
|---|---|
pinch.scale | scale on the event object, or pinch.scale |
pinch.velocity | velocity on the event object, or pinch.velocity |
See also
- Responding to gestures
CGAffineTransform: theCGAffineTransformthat applies the multiple to a viewUIGestureRecognizer: the parent class, with the events and shared properties