Skip to content

Instantly share code, notes, and snippets.

@hupptechnologies
Created May 30, 2020 09:52
Show Gist options
  • Save hupptechnologies/e347ec440dc6389b842397feecbba1c7 to your computer and use it in GitHub Desktop.
Save hupptechnologies/e347ec440dc6389b842397feecbba1c7 to your computer and use it in GitHub Desktop.
Pinch to zoom gesture in React Native
import React from "react"
import PropTypes from "prop-types";
import { Animated } from "react-native"
import { PinchGestureHandler, State } from "react-native-gesture-handler"
export default function ImageView(props) {
const scale = new Animated.Value(1)
const onZoomEvent = Animated.event([
{
nativeEvent: { scale: scale }
}
],{
useNativeDriver: true
});
const onZoomStateChange = event => {
if (event.nativeEvent.oldState === State.ACTIVE) {
Animated.spring(scale, {
toValue: 1,
useNativeDriver: true
}).start()
}
}
const { style, source } = props;
return (
<PinchGestureHandler
onGestureEvent={onZoomEvent}
onHandlerStateChange={onZoomStateChange}
>
<Animated.Image
source={source}
style={[style, { transform: [{ scale: scale }] }]}
resizeMode="contain"
/>
</PinchGestureHandler>
)
}
ImageView.propTypes = {
source: PropTypes.oneOfType([
PropTypes.shape({
uri: PropTypes.string,
headers: PropTypes.objectOf(PropTypes.string)
}),
PropTypes.number,
PropTypes.arrayOf(
PropTypes.shape({
uri: PropTypes.string,
width: PropTypes.number,
height: PropTypes.number,
headers: PropTypes.objectOf(PropTypes.string)
})
)
]).isRequired,
style: PropTypes.object
};
/* SPECIFIES THE DEFAULT VALUES FOR PROPS */
ImageView.defaultProps = {
style: {}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment