The DismissibleBanner moved by calling a state setter on every touch move, which re-renders the component each time. Animated can take the gesture numbers directly: Animated.event writes gestureState.dx and dy into animated values, and a transform bound to them follows the finger. The release needs more work. dx starts from zero again on every drag, so the box needs an offset to stay where it was dropped.

Key terms

  • Animated.ValueXY: a 2D animated value made of two regular Animated.Values, x and y, for things like pan gestures.
  • setOffset: sets an offset that is added on top of whatever base value setValue, an animation or Animated.event writes.
  • extractOffset: moves the current base value into the offset and sets the base value to zero.
  • flattenOffset: merges the offset into the base value and resets the offset to zero.

Two axes in one value

new Animated.ValueXY() starts at {x: 0, y: 0} and holds two Animated.Values, pan.x and pan.y. Its API is almost the same as Animated.Value: setValue, setOffset, flattenOffset and extractOffset call the same method on both axes. To put it on screen, use pan.x and pan.y as translateX and translateY in a transform. Two helpers build the style for you: getTranslateTransform() returns that transform array, and getLayout() returns {left, top}.

Mapping gestureState onto the value

Animated.event takes an array with one entry per argument of the handler it builds. PanResponder calls onPanResponderMove with (event, gestureState), so the DraggableSticker component below maps the second argument:

onPanResponderMove: Animated.event([null, {dx: pan.x, dy: pan.y}], {useNativeDriver: false})

The null skips the native event, and the object sends gestureState.dx to pan.x and dy to pan.y. On each move the handler reads those numbers and calls setValue on the mapped values. A mapped key that gestureState doesn't have, such as a misspelled dX, gives its value no number, and in development the handler throws an error that starts with Bad mapping of event key.

Here useNativeDriver must be false, because the native driver's Animated.event only handles direct events and onPanResponderMove is not one.

Where the second drag starts

An Animated.Value's output is its base value plus its offset. Animated.event writes through setValue, which updates only the base value and leaves the offset alone. Combine that with the reset of dx to 0 each time the view becomes the responder: drop the box 120 to the right, touch it again, and the first move writes a small dx into pan.x, so the box jumps back near its starting point.

DraggableSticker calls pan.extractOffset() on release to prevent the jump. extractOffset adds the current base value to the existing offset and sets the base value to 0, so the output doesn't change at release, and the next drag's dx lands on top of everything before it. The AnimatedValue reference describes it as setting the offset to the base value, which would forget every drag but the last. The implementation adds, and that is what lets a box dragged several times keep its place. Termination ends a drag too, so onPanResponderTerminate gets the same call.

The other two methods are for building or clearing offsets by hand. setOffset replaces the offset outright, and flattenOffset folds the offset back into the base value without moving the box.

Springing back on release

For a box that returns home instead, run a spring when the finger lifts. Animated.spring accepts a ValueXY directly with toValue: {x: 0, y: 0}, splits each {x, y} option into an x and a y animation, and runs the two in parallel.

An animation starts from and writes to the base value, so the offset is still added where it lands. After earlier drags have called extractOffset, a spring to zero stops at the last drop point. The release handler below calls flattenOffset first, so the spring sees the full position and ends at the origin. It keeps useNativeDriver: false as well. A native spring would move pan to the native side, and from then on any JS-driven animation you start on pan, such as a JS spring or timing, throws.

Code Examples

tsx
import {useRef} from 'react';
import {Animated, PanResponder, StyleSheet} from 'react-native';

export function DraggableSticker() {
  const pan = useRef(new Animated.ValueXY()).current;

  const panResponder = useRef(
    PanResponder.create({
      onMoveShouldSetPanResponder: () => true,
      onPanResponderMove: Animated.event([null, {dx: pan.x, dy: pan.y}], {
        useNativeDriver: false,
      }),
      onPanResponderRelease: () => {
        pan.extractOffset();
      },
      onPanResponderTerminate: () => {
        pan.extractOffset();
      },
    }),
  ).current;

  return (
    <Animated.View
      style={[
        styles.sticker,
        {transform: [{translateX: pan.x}, {translateY: pan.y}]},
      ]}
      {...panResponder.panHandlers}
    />
  );
}

const styles = StyleSheet.create({
  sticker: {
    width: 96,
    height: 96,
    borderRadius: 16,
    backgroundColor: '#f59e0b',
  },
});
tsx
// Fragment: replaces onPanResponderRelease inside PanResponder.create
// in DraggableSticker. Imports and the rest of the component are unchanged.
onPanResponderRelease: () => {
  pan.flattenOffset();
  Animated.spring(pan, {
    toValue: {x: 0, y: 0},
    useNativeDriver: false,
  }).start();
},
✓ Completed