UIPanGestureRecognizer: setTranslation() method
The setTranslation() method sets the recognizer's accumulated translation to a given value. Later movement keeps accumulating from that value.
Syntax
setTranslation(translation, { inView })Parameters
translation
The new translation, a CGPoint or { x, y }, in the coordinate system of inView, in points (pt).
options.inView
Required. A UIView or null; null means the window's coordinate system.
Return value
None (undefined).
Exceptions
TypeError
inViewis missing:setTranslation needs { inView }translationis missing a field, or a field isn't a number:setTranslation is missing y, or it is not a finite numberinViewis neither a view nornull.
Description
- Calling it while no gesture is in progress has no effect; it still reads back as
(0, 0). - The recommended way to drag doesn't need it: record the view's starting position in
began, and inchangedcompute the new position as "starting position + the event'stranslation". See Dragging a view.
Examples
js
const card = new UIView();
const drag = new UIPanGestureRecognizer();
drag.addEventListener('changed', () => {
const moved = drag.translationInView(card.superview);
card.center = new CGPoint(card.center.x + moved.x, card.center.y + moved.y);
drag.setTranslation(new CGPoint(0, 0), { inView: card.superview });
});
card.addGestureRecognizer(drag);Effect: The translation is reset to zero after each move, so the next read gives the translation of just that small step, and card follows the finger. Not verified in the simulator.
Corresponding iOS API
-[UIPanGestureRecognizer setTranslation:inView:]. [pan setTranslation:CGPointZero inView:view] is written as pan.setTranslation(new CGPoint(0, 0), { inView: view }).