-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add scale animated value & return last values in onResetAnimati…
…onEnd callback (#59) * feat: add scale animated value & return last values in onResetAnimationEnd callback * chore: update docs
- Loading branch information
1 parent
f23e4a7
commit 104d6e9
Showing
14 changed files
with
416 additions
and
103 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,160 @@ | ||
import React, { useRef, useState } from 'react'; | ||
import { StyleSheet, View, Pressable, Text } from 'react-native'; | ||
import { gestureHandlerRootHOC } from 'react-native-gesture-handler'; | ||
import Animated, { | ||
useSharedValue, | ||
FadeIn, | ||
FadeOut, | ||
} from 'react-native-reanimated'; | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
import { ImageZoomRef } from '../../src'; | ||
import { AnimatedCircle } from './components/AnimatedCircle'; | ||
import ExpoImageZoom from './components/ExpoImageZoom'; | ||
import ImageZoom from './components/ImageZoom'; | ||
import safeAreaContextProviderHOC from './safeAreaContextProviderHOC'; | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
image: { | ||
overflow: 'hidden', | ||
}, | ||
button: { | ||
zIndex: 10, | ||
position: 'absolute', | ||
right: 8, | ||
height: 40, | ||
justifyContent: 'center', | ||
backgroundColor: 'rgba(255, 255, 255, 0.64)', | ||
paddingHorizontal: 16, | ||
paddingVertical: 8, | ||
borderWidth: 2, | ||
borderRadius: 20, | ||
borderColor: 'yellow', | ||
}, | ||
zoomInButton: { | ||
bottom: 48, | ||
}, | ||
zoomOutButton: { | ||
top: 48, | ||
}, | ||
switchComponentButton: { | ||
right: undefined, | ||
left: 8, | ||
}, | ||
circle: { | ||
position: 'absolute', | ||
top: 491, | ||
left: 146, | ||
width: 24, | ||
height: 24, | ||
borderWidth: 2, | ||
borderRadius: 12, | ||
borderColor: 'yellow', | ||
transform: [{ translateX: -12 }, { translateY: -12 }], | ||
}, | ||
buttonText: { | ||
fontWeight: 'bold', | ||
}, | ||
}); | ||
import { COLORS } from './themes/colors'; | ||
|
||
// Photo by Walling [https://unsplash.com/photos/XLqiL-rz4V8] on Unsplash [https://unsplash.com/] | ||
const imageUri = 'https://images.unsplash.com/photo-1596003906949-67221c37965c'; | ||
const IMAGE_URI = | ||
'https://images.unsplash.com/photo-1596003906949-67221c37965c'; | ||
const MIN_SCALE = 0.5; | ||
const MAX_SCALE = 5; | ||
const ZOOM_IN_X = 146; | ||
const ZOOM_IN_Y = 491; | ||
|
||
const AnimatedPressable = Animated.createAnimatedComponent(Pressable); | ||
const FadeInAnimation = FadeIn.duration(256); | ||
const FadeOutAnimation = FadeOut.duration(256); | ||
|
||
function App() { | ||
const imageZoomRef = useRef<ImageZoomRef>(null); | ||
const { top, bottom } = useSafeAreaInsets(); | ||
|
||
const scale = useSharedValue(1); | ||
|
||
const [useCustomComponent, setUseCustomComponent] = useState(false); | ||
const [isZoomed, setIsZoomed] = useState(false); | ||
|
||
const toggleComponent = () => { | ||
setUseCustomComponent((current) => !current); | ||
}; | ||
const zoomIn = () => { | ||
imageZoomRef?.current?.zoom({ scale: 5, x: ZOOM_IN_X, y: ZOOM_IN_Y }); | ||
}; | ||
const zoomOut = () => { | ||
imageZoomRef?.current?.reset(); | ||
}; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
{useCustomComponent ? ( | ||
<ExpoImageZoom | ||
ref={imageZoomRef} | ||
uri={imageUri} | ||
uri={IMAGE_URI} | ||
scale={scale} | ||
minScale={MIN_SCALE} | ||
maxScale={MAX_SCALE} | ||
setIsZoomed={setIsZoomed} | ||
/> | ||
) : ( | ||
<ImageZoom | ||
ref={imageZoomRef} | ||
uri={imageUri} | ||
uri={IMAGE_URI} | ||
scale={scale} | ||
minScale={MIN_SCALE} | ||
maxScale={MAX_SCALE} | ||
setIsZoomed={setIsZoomed} | ||
/> | ||
)} | ||
<Pressable | ||
onPress={() => { | ||
setUseCustomComponent((current) => !current); | ||
}} | ||
style={[styles.button, styles.switchComponentButton, { top: top + 16 }]} | ||
<AnimatedPressable | ||
onPress={toggleComponent} | ||
entering={FadeInAnimation} | ||
exiting={FadeOutAnimation} | ||
style={[styles.button, styles.switchComponentButton, { top: top + 8 }]} | ||
> | ||
<Text style={styles.buttonText}> | ||
Use {useCustomComponent ? 'React Native Image' : 'Expo Image'} | ||
</Text> | ||
</Pressable> | ||
</AnimatedPressable> | ||
|
||
{isZoomed ? ( | ||
<Pressable | ||
onPress={() => { | ||
imageZoomRef?.current?.reset(); | ||
}} | ||
style={[styles.button, { top: top + 16 }]} | ||
> | ||
<Text style={styles.buttonText}>Zoom Out</Text> | ||
</Pressable> | ||
<> | ||
<AnimatedPressable | ||
onPress={zoomOut} | ||
entering={FadeInAnimation} | ||
exiting={FadeOutAnimation} | ||
style={[styles.button, { top: top + 8 }]} | ||
> | ||
<Text style={styles.buttonText}>Zoom Out</Text> | ||
</AnimatedPressable> | ||
<AnimatedCircle | ||
size={50} | ||
scale={scale} | ||
minScale={1} | ||
maxScale={MAX_SCALE} | ||
entering={FadeInAnimation} | ||
exiting={FadeOutAnimation} | ||
style={[styles.progressCircle, { bottom: bottom + 8 }]} | ||
/> | ||
</> | ||
) : ( | ||
<> | ||
<View pointerEvents="none" style={styles.circle} /> | ||
<Pressable | ||
onPress={() => { | ||
imageZoomRef?.current?.zoom({ scale: 5, x: 146, y: 491 }); | ||
}} | ||
style={[styles.button, { bottom: bottom + 16 }]} | ||
<View pointerEvents="none" style={styles.zoomInCircle} /> | ||
<AnimatedPressable | ||
onPress={zoomIn} | ||
entering={FadeInAnimation} | ||
exiting={FadeOutAnimation} | ||
style={[styles.button, { bottom: bottom + 8 }]} | ||
> | ||
<Text style={styles.buttonText}>Zoom In the 🟡 Circle</Text> | ||
</Pressable> | ||
</AnimatedPressable> | ||
</> | ||
)} | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
button: { | ||
position: 'absolute', | ||
zIndex: 10, | ||
right: 8, | ||
height: 40, | ||
justifyContent: 'center', | ||
backgroundColor: COLORS.mainDarkAlpha(0.16), | ||
paddingHorizontal: 16, | ||
paddingVertical: 8, | ||
borderWidth: 2, | ||
borderRadius: 20, | ||
borderColor: COLORS.accent, | ||
}, | ||
switchComponentButton: { | ||
right: undefined, | ||
left: 8, | ||
}, | ||
zoomInCircle: { | ||
position: 'absolute', | ||
top: ZOOM_IN_Y, | ||
left: ZOOM_IN_X, | ||
width: 24, | ||
height: 24, | ||
borderWidth: 2, | ||
borderRadius: 12, | ||
borderColor: COLORS.accent, | ||
transform: [{ translateX: -12 }, { translateY: -12 }], | ||
}, | ||
buttonText: { | ||
fontWeight: 'bold', | ||
color: COLORS.white, | ||
}, | ||
progressCircle: { | ||
position: 'absolute', | ||
left: 16, | ||
bottom: 16, | ||
}, | ||
}); | ||
|
||
export default safeAreaContextProviderHOC(gestureHandlerRootHOC(App)); |
Oops, something went wrong.