Skip to main content

Lesson 3 of 9

Gesture Handler - Pan, Pinch & Long Press

React Native Gesture Handler replaces the JS-thread touch system with native gesture recognizers. Gestures are calculated in native code, so they can't be blocked by a long JS frame and don't drop frames during heavy renders.

30 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Installation

Installation
npx expo install react-native-gesture-handler
# Wrap root with <GestureHandlerRootView style={{ flex: 1 }}>

Pan + Pinch (Zoomable Image)

Pan + Pinch (Zoomable Image)
import Animated, { useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';
import { Gesture, GestureDetector, GestureHandlerRootView } from 'react-native-gesture-handler';
import { StyleSheet, View } from 'react-native';

export function ZoomableImage({ uri }: { uri: string }) {
  const scale = useSharedValue(1);
  const savedScale = useSharedValue(1);
  const translateX = useSharedValue(0);
  const translateY = useSharedValue(0);
  const savedTranslateX = useSharedValue(0);
  const savedTranslateY = useSharedValue(0);

  // ---- Pinch to zoom ----
  const pinch = Gesture.Pinch()
    .onUpdate(event => {
      scale.value = Math.max(1, savedScale.value * event.scale);
    })
    .onEnd(() => {
      savedScale.value = scale.value;
      if (scale.value < 1.05) {
        // Snap back to 1ร— if pinched too small
        scale.value = withSpring(1);
        savedScale.value = 1;
      }
    });

  // ---- Pan to drag (only works when zoomed) ----
  const pan = Gesture.Pan()
    .onUpdate(event => {
      if (scale.value > 1) {
        translateX.value = savedTranslateX.value + event.translationX;
        translateY.value = savedTranslateY.value + event.translationY;
      }
    })
    .onEnd(() => {
      savedTranslateX.value = translateX.value;
      savedTranslateY.value = translateY.value;
    });

  // ---- Double tap to reset ----
  const doubleTap = Gesture.Tap()
    .numberOfTaps(2)
    .onEnd(() => {
      scale.value = withSpring(1);
      savedScale.value = 1;
      translateX.value = withSpring(0);
      translateY.value = withSpring(0);
      savedTranslateX.value = 0;
      savedTranslateY.value = 0;
    });

  // Combine gestures: pinch and pan simultaneously, double-tap exclusive
  const composed = Gesture.Simultaneous(
    Gesture.Exclusive(doubleTap, pan),
    pinch
  );

  const animatedStyle = useAnimatedStyle(() => ({
    transform: [
      { translateX: translateX.value },
      { translateY: translateY.value },
      { scale: scale.value },
    ],
  }));

  return (
    <GestureDetector gesture={composed}>
      <Animated.Image
        source={{ uri }}
        style={[StyleSheet.absoluteFillObject, animatedStyle]}
        resizeMode="contain"
      />
    </GestureDetector>
  );
}

Tip

Tip

Practice Gesture Handler Pan Pinch Long Press in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagramโ€ฆ

Handling complex Pan, Pinch, and multi-touch gestures simultaneously

Practice Task

Note

Practice Task - (1) Write a working example of Gesture Handler Pan Pinch Long Press from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Gesture Handler Pan Pinch Long Press is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/9 done