Lift the swipe animated value upwards
This commit is contained in:
@@ -3,6 +3,7 @@ import {ActivityIndicator, Dimensions, StyleSheet} from 'react-native'
|
|||||||
import {Gesture, GestureDetector} from 'react-native-gesture-handler'
|
import {Gesture, GestureDetector} from 'react-native-gesture-handler'
|
||||||
import Animated, {
|
import Animated, {
|
||||||
runOnJS,
|
runOnJS,
|
||||||
|
SharedValue,
|
||||||
useAnimatedReaction,
|
useAnimatedReaction,
|
||||||
useAnimatedRef,
|
useAnimatedRef,
|
||||||
useAnimatedStyle,
|
useAnimatedStyle,
|
||||||
@@ -43,6 +44,7 @@ type Props = {
|
|||||||
onZoom: (isZoomed: boolean) => void
|
onZoom: (isZoomed: boolean) => void
|
||||||
isScrollViewBeingDragged: boolean
|
isScrollViewBeingDragged: boolean
|
||||||
showControls: boolean
|
showControls: boolean
|
||||||
|
dismissSwipeTranslateY: SharedValue<number>
|
||||||
}
|
}
|
||||||
const ImageItem = ({
|
const ImageItem = ({
|
||||||
imageSrc,
|
imageSrc,
|
||||||
@@ -50,6 +52,7 @@ const ImageItem = ({
|
|||||||
onZoom,
|
onZoom,
|
||||||
onRequestClose,
|
onRequestClose,
|
||||||
isScrollViewBeingDragged,
|
isScrollViewBeingDragged,
|
||||||
|
dismissSwipeTranslateY,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const [isScaled, setIsScaled] = useState(false)
|
const [isScaled, setIsScaled] = useState(false)
|
||||||
const [imageAspect, imageDimensions] = useImageDimensions({
|
const [imageAspect, imageDimensions] = useImageDimensions({
|
||||||
@@ -61,7 +64,6 @@ const ImageItem = ({
|
|||||||
const pinchOrigin = useSharedValue({x: 0, y: 0})
|
const pinchOrigin = useSharedValue({x: 0, y: 0})
|
||||||
const pinchScale = useSharedValue(1)
|
const pinchScale = useSharedValue(1)
|
||||||
const pinchTranslation = useSharedValue({x: 0, y: 0})
|
const pinchTranslation = useSharedValue({x: 0, y: 0})
|
||||||
const dismissSwipeTranslateY = useSharedValue(0)
|
|
||||||
const containerRef = useAnimatedRef()
|
const containerRef = useAnimatedRef()
|
||||||
|
|
||||||
// Keep track of when we're entering or leaving scaled rendering.
|
// Keep track of when we're entering or leaving scaled rendering.
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import {Gesture, GestureDetector} from 'react-native-gesture-handler'
|
|||||||
import Animated, {
|
import Animated, {
|
||||||
interpolate,
|
interpolate,
|
||||||
runOnJS,
|
runOnJS,
|
||||||
|
SharedValue,
|
||||||
useAnimatedRef,
|
useAnimatedRef,
|
||||||
useAnimatedStyle,
|
useAnimatedStyle,
|
||||||
useSharedValue,
|
|
||||||
} from 'react-native-reanimated'
|
} from 'react-native-reanimated'
|
||||||
import {Image} from 'expo-image'
|
import {Image} from 'expo-image'
|
||||||
|
|
||||||
@@ -35,6 +35,7 @@ type Props = {
|
|||||||
onZoom: (scaled: boolean) => void
|
onZoom: (scaled: boolean) => void
|
||||||
isScrollViewBeingDragged: boolean
|
isScrollViewBeingDragged: boolean
|
||||||
showControls: boolean
|
showControls: boolean
|
||||||
|
dismissSwipeTranslateY: SharedValue<number>
|
||||||
}
|
}
|
||||||
|
|
||||||
const ImageItem = ({
|
const ImageItem = ({
|
||||||
@@ -43,9 +44,10 @@ const ImageItem = ({
|
|||||||
onZoom,
|
onZoom,
|
||||||
onRequestClose,
|
onRequestClose,
|
||||||
showControls,
|
showControls,
|
||||||
|
dismissSwipeTranslateY,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const scrollViewRef = useAnimatedRef<Animated.ScrollView>()
|
const scrollViewRef = useAnimatedRef<Animated.ScrollView>()
|
||||||
const translationY = useSharedValue(0)
|
|
||||||
const [scaled, setScaled] = useState(false)
|
const [scaled, setScaled] = useState(false)
|
||||||
const [imageAspect, imageDimensions] = useImageDimensions({
|
const [imageAspect, imageDimensions] = useImageDimensions({
|
||||||
src: imageSrc.uri,
|
src: imageSrc.uri,
|
||||||
@@ -58,7 +60,7 @@ const ImageItem = ({
|
|||||||
const animatedStyle = useAnimatedStyle(() => {
|
const animatedStyle = useAnimatedStyle(() => {
|
||||||
return {
|
return {
|
||||||
opacity: interpolate(
|
opacity: interpolate(
|
||||||
translationY.value,
|
dismissSwipeTranslateY.value,
|
||||||
[-SWIPE_CLOSE_OFFSET, 0, SWIPE_CLOSE_OFFSET],
|
[-SWIPE_CLOSE_OFFSET, 0, SWIPE_CLOSE_OFFSET],
|
||||||
[0.5, 1, 0.5],
|
[0.5, 1, 0.5],
|
||||||
),
|
),
|
||||||
@@ -68,7 +70,7 @@ const ImageItem = ({
|
|||||||
const scrollHandler = useAnimatedScrollHandler({
|
const scrollHandler = useAnimatedScrollHandler({
|
||||||
onScroll(e) {
|
onScroll(e) {
|
||||||
const nextIsScaled = e.zoomScale > 1
|
const nextIsScaled = e.zoomScale > 1
|
||||||
translationY.value = nextIsScaled ? 0 : e.contentOffset.y
|
dismissSwipeTranslateY.value = nextIsScaled ? 0 : e.contentOffset.y
|
||||||
if (scaled !== nextIsScaled) {
|
if (scaled !== nextIsScaled) {
|
||||||
runOnJS(handleZoom)(nextIsScaled)
|
runOnJS(handleZoom)(nextIsScaled)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {View} from 'react-native'
|
import {View} from 'react-native'
|
||||||
|
import {SharedValue} from 'react-native-reanimated'
|
||||||
|
|
||||||
import {ImageSource} from '../../@types'
|
import {ImageSource} from '../../@types'
|
||||||
|
|
||||||
@@ -12,6 +13,7 @@ type Props = {
|
|||||||
onZoom: (scaled: boolean) => void
|
onZoom: (scaled: boolean) => void
|
||||||
isScrollViewBeingDragged: boolean
|
isScrollViewBeingDragged: boolean
|
||||||
showControls: boolean
|
showControls: boolean
|
||||||
|
dismissSwipeTranslateY: SharedValue<number>
|
||||||
}
|
}
|
||||||
|
|
||||||
const ImageItem = (_props: Props) => {
|
const ImageItem = (_props: Props) => {
|
||||||
|
|||||||
@@ -18,7 +18,11 @@ import {
|
|||||||
} from 'react-native'
|
} from 'react-native'
|
||||||
import PagerView from 'react-native-pager-view'
|
import PagerView from 'react-native-pager-view'
|
||||||
import {MeasuredDimensions} from 'react-native-reanimated'
|
import {MeasuredDimensions} from 'react-native-reanimated'
|
||||||
import Animated, {useAnimatedStyle, withSpring} from 'react-native-reanimated'
|
import Animated, {
|
||||||
|
useAnimatedStyle,
|
||||||
|
useSharedValue,
|
||||||
|
withSpring,
|
||||||
|
} from 'react-native-reanimated'
|
||||||
import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
||||||
import {Edge, SafeAreaView} from 'react-native-safe-area-context'
|
import {Edge, SafeAreaView} from 'react-native-safe-area-context'
|
||||||
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
|
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
|
||||||
@@ -62,6 +66,7 @@ function ImageViewing({
|
|||||||
const [imageIndex, setImageIndex] = useState(initialImageIndex)
|
const [imageIndex, setImageIndex] = useState(initialImageIndex)
|
||||||
const [showControls, setShowControls] = useState(true)
|
const [showControls, setShowControls] = useState(true)
|
||||||
|
|
||||||
|
const dismissSwipeTranslateY = useSharedValue(0)
|
||||||
const animatedHeaderStyle = useAnimatedStyle(() => ({
|
const animatedHeaderStyle = useAnimatedStyle(() => ({
|
||||||
pointerEvents: showControls ? 'auto' : 'none',
|
pointerEvents: showControls ? 'auto' : 'none',
|
||||||
opacity: withClampedSpring(showControls ? 1 : 0),
|
opacity: withClampedSpring(showControls ? 1 : 0),
|
||||||
@@ -134,6 +139,7 @@ function ImageViewing({
|
|||||||
onRequestClose={onRequestClose}
|
onRequestClose={onRequestClose}
|
||||||
isScrollViewBeingDragged={isDragging}
|
isScrollViewBeingDragged={isDragging}
|
||||||
showControls={showControls}
|
showControls={showControls}
|
||||||
|
dismissSwipeTranslateY={dismissSwipeTranslateY}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user