Skip to content

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 ​

UIPinchGestureRecognizer()

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

PropertyDescription
scaleThe distance between the two fingers as a multiple of the distance when the gesture began
velocity read-onlyHow much scale changes per second

Events ​

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

EventWhen it fires
beganWhen a pinch is recognized
changedEach time the distance between the fingers changes after that
endedWhen the fingers lift
cancelledWhen the gesture is interrupted

Examples ​

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

ObjCJS
pinch.scalescale on the event object, or pinch.scale
pinch.velocityvelocity on the event object, or pinch.velocity

See also ​