DEV Community

Cover image for How to use Pinch to Zoom Gesture in React Native apps
Aman Mittal
Aman Mittal

Posted on

How to use Pinch to Zoom Gesture in React Native apps

The open-source library react-native-gesture-handler is a great way to add gestures to cross-platform React Native apps. Two of the main reasons I find this useful because it uses native support to handle gestures and the other reason being it is better at performing on each native platform than React Native's built-in touch system Gesture Responder system.

In this tutorial, let us explore this library by creating a small demo that allows the user to use their two fingers to pinch in to zoom a media content. For the media content, I am going to use a placeholder image. This pinch gesture is achievable by using PinchGestureHandler from the library. This handler tracks the distance between two fingers and uses that information to scale or zoom on the content. It gets activated when the fingers are placed on the screen and when their position changes.

Table of contents

  • Requirements
  • Setting up react-native-gesture-handler
  • Set up App component to display an image
  • Using dynamic Image component with Animated API
  • Adding Animated event and state change handler
  • Conclusion

Requirements

  • Node.js >= 10.x.xversion installed
  • watchman
  • react-native-cli

Do note that Iā€™m going to use an iOS simulator for this tutorial.

Setting up react-native-gesture-handler

To get started, create a bare react native project using react-native CLI by running the below commands from a terminal window.

react-native init pinchZoomGesture

# after the project directory is created
# and dependencies are installed
cd pinchZoomGesture
Enter fullscreen mode Exit fullscreen mode

The react-native-gesture-handler supports both react-native CLI projects and Expo projects. To install it, execute the below command:

yarn add react-native-gesture-handler
Enter fullscreen mode Exit fullscreen mode

For the current demo, since you are using react-native CLI, only Android users have to add the following configuration MainActivity.java file.

package com.swipegesturesdemo;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

public class MainActivity extends ReactActivity {

 /**
 * Returns the name of the main component registered from JavaScript. This is used to schedule
 * rendering of the component.
 */
 @Override
 protected String getMainComponentName() {
 return "swipeGesturesDemo";
 }

 @Override
 protected ReactActivityDelegate createReactActivityDelegate() {
 return new ReactActivityDelegate(this, getMainComponentName()) {
 @Override
 protected ReactRootView createRootView() {
 return new RNGestureHandlerEnabledRootView(MainActivity.this);
 }
 };
 }
}
Enter fullscreen mode Exit fullscreen mode

For iOS users, navigate inside ios/ directory from the terminal and run pod install.

Everything is set up, all you have to do is run the build command again, such as for iOS: react-native run-ios and for Android: react-native run-android.

Set up App component to display an image

In this section, let us quickly set up the App component to display a placeholder image. You can use any image as a placeholder. Here is the snippet for the App.js file to get started.

import React from 'react'
import { Image, View, Dimensions } from 'react-native'

const { width } = Dimensions.get('window')

const App = () => {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Image
        source={{
          uri: 'https://miro.medium.com/max/1080/1*7SYuZvH2pZnM0H79V4ttPg.jpeg'
        }}
        style={{
          width: width,
          height: 300
        }}
        resizeMode="contain"
      />
    </View>
  )
}

export default App
Enter fullscreen mode Exit fullscreen mode

It uses the width of the device's screen to calculate the width of the image using Dimensions from react-native. To run this demo for the first time build the app for the platform you are using:

  • for iOS, run: react-native run-ios
  • for Android, run: react-native run-android

Here is the output when the app runs for the first time.

hb1

Using dynamic Image component with Animated API

Animated.Image is going to serve the purpose of displaying an image as well as perform scale animations.

Animated API uses declarative relationships between input and output values. For single values, you can use Animated.Value(). It is required since it is going to be a style property initially.

Start by importing Animated from react-native and replace the Image with Animated.Image.

import { View, Dimensions, Animated } from 'react-native'

// in return statement
return (
  <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    <Animated.Image
      source={{
        uri: 'https://miro.medium.com/max/1080/1*7SYuZvH2pZnM0H79V4ttPg.jpeg'
      }}
      style={{
        width: width,
        height: 300,
        transform: [{ scale: 1 }]
      }}
      resizeMode="contain"
    />
  </View>
)
Enter fullscreen mode Exit fullscreen mode

Also, by mentioning the value of the scale to one, it is going to display the image as usual.

Now, wrap the Animated.Image with PinchGestureHandler. Ths wrapper component is going to have two props.

return (
  <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    <PinchGestureHandler
      onGestureEvent={this.onZoomEvent}
      onHandlerStateChange={this.onZoomStateChange}
    >
      <Animated.Image
        source={{
          uri: 'https://miro.medium.com/max/1080/1*7SYuZvH2pZnM0H79V4ttPg.jpeg'
        }}
        style={{
          width: width,
          height: 300,
          transform: [{ scale: this.scale }]
        }}
        resizeMode="contain"
      />
    </PinchGestureHandler>
  </View>
)
Enter fullscreen mode Exit fullscreen mode

Adding Animated event and state change handler

Let us define the onZoomEvent first, before the return statement. This event is going to be an Animated event. This way gestures can directly map to animated values. The animated value to be used here is scale.

Passing useNativeDriver as boolean true allows the animations to happen on the native thread instead of JavaScript thread. This helps with performance.

scale = new Animated.Value(1)

onZoomEvent = Animated.event(
  [
    {
      nativeEvent: { scale: this.scale }
    }
  ],
  {
    useNativeDriver: true
  }
)
Enter fullscreen mode Exit fullscreen mode

Now define the handler method onZoomStateChange that handles the state change when the gesture is over. Each gesture handler has is assigned a state that changes when a new touch event occurs.

There are different possible states for every handler but for the current gesture handler, ACTIVE is used to check whether the event is still active or not. To access these states, the object is required to import from the library itself.

The Animated.spring on scale property has toValue set to 1 which is the initial scale value when the animation is done.

onZoomStateChange = event => {
  if (event.nativeEvent.oldState === State.ACTIVE) {
    Animated.spring(this.scale, {
      toValue: 1,
      useNativeDriver: true
    }).start()
  }
}
Enter fullscreen mode Exit fullscreen mode

This completes all the configuration and handler required. Look at the demo below to see it in action.

hb2

Conclusion

This completes the tutorial on how to use one of the gestures from the react-native-gesture-handler library. I recommend you to go through its official documentation and methods and try other gestures as well.

You can find the complete code at this GitHub repo.

Originally published at Heartbeat.fritz.ai


I frequently write on Nodejs, Reactjs, and React Native. You can subscribe to my weekly newsletter to receive new updates straight to your inbox šŸ’Œ

Top comments (6)

Collapse
 
swapanil2021 profile image
swapanil2021

Hi @amanhimself ,

If i pinch second time it firstly get scale to 1 then scale accordingly.

I like the functionality as in image attached, will great if you help.

Collapse
 
bogdanrad profile image
BogdanRad

Hey Aman, on ios all is good but on android is not working :(. Do you know where is the problem?

Collapse
 
amanhimself profile image
Aman Mittal

Let me check.

Collapse
 
bogdanrad profile image
BogdanRad

Thanks dude

Thread Thread
 
anisha95 profile image
Anisha Pal

Have you got the solution?

Collapse
 
bogdanrad profile image
BogdanRad

I found the problem dude. In modal is not working... After I removed the modal all is good.
How can I move left and right the image?