Created
May 30, 2020 09:52
-
-
Save hupptechnologies/e347ec440dc6389b842397feecbba1c7 to your computer and use it in GitHub Desktop.
Pinch to zoom gesture in React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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