Intro
This is a pan and pinch component for React Native that
- handles touch inputs (zooming and panning)
- limits panning and zooming to boundaries (min and max movements on x and y axis, min and max zoom levels) and
- sets all required attributes on children to transform them correspondingly.
Prerequisites
- Your React Native setup must be linked to React Native Reanimated.
- If you use an up-to-date version of Expo, React Native Reanimated is preinstalled and already linked.
Usage
-
Create a child element that will be transformed:
// Box.js;;;Component{return<AnimatedViewstyle=stylesbox// props.animatedLeft, props.animatedTop and props.animatedZoom were added// to this component by PanPinch. They are all of type Animated.Node.transform:translateX: thispropsanimatedLefttranslateY: thispropsanimatedTopscale: thispropsanimatedZoom/>;}const styles = StyleSheet; -
Create a parent component that renders PanPinch and children you'd like to transform.
// App.js;;;;Componentstate =dimensions:{const width height = evnativeEventlayout;this;}{return/* In this case, we want this view to serve as the boundaries for Box. Thereforewe have to view its layout change and update containerDimensions on PanPinchaccordingly */<Viewstyle=stylescontaineronLayout= this><PanPinch containerDimensions=thisstatedimensions><Box /></PanPinch></View>;}const styles = StyleSheet;
API
Props
- containerDimensions: Takes an array of 2 numbers (width and height of the boundaries in which
the PanPinch's content can move), e.g.
[200, 400]
if we want to restrict x pan to 200 and y pan to 400 px.
Limitations
- Boundaries cannot yet be set; will be added as soon as Expo uses a newer version of React Native Reanimated (setValue doesn't work yet).
- Zoom level is not yet respected for movement boundaries on x and y axis.