Work around the pinch/scroll conflict

This commit is contained in:
Dan Abramov
2023-10-03 04:48:35 +01:00
parent 23a8cb9744
commit 115d60d616
2 changed files with 19 additions and 1 deletions
@@ -156,6 +156,7 @@ const ImageItem = ({
delayLongPress, delayLongPress,
swipeToCloseEnabled = true, swipeToCloseEnabled = true,
doubleTapToZoomEnabled = true, doubleTapToZoomEnabled = true,
gestureRef,
}: Props) => { }: Props) => {
const [isScaled, setIsScaled] = useState(false) const [isScaled, setIsScaled] = useState(false)
const [isLoaded, setIsLoaded] = useState(false) const [isLoaded, setIsLoaded] = useState(false)
@@ -223,6 +224,7 @@ const ImageItem = ({
} }
const pinch = Gesture.Pinch() const pinch = Gesture.Pinch()
.withRef(gestureRef)
.onStart((e) => { .onStart((e) => {
pinchOrigin.value = { pinchOrigin.value = {
x: e.focalX - SCREEN.width/2, x: e.focalX - SCREEN.width/2,
+17 -1
View File
@@ -10,10 +10,12 @@
import React, { import React, {
ComponentType, ComponentType,
createRef,
useCallback, useCallback,
useRef, useRef,
useEffect, useEffect,
useMemo, useMemo,
useState,
} from 'react' } from 'react'
import { import {
Animated, Animated,
@@ -33,6 +35,7 @@ import useAnimatedComponents from './hooks/useAnimatedComponents'
import useImageIndexChange from './hooks/useImageIndexChange' import useImageIndexChange from './hooks/useImageIndexChange'
import useRequestClose from './hooks/useRequestClose' import useRequestClose from './hooks/useRequestClose'
import {ImageSource} from './@types' import {ImageSource} from './@types'
import {ScrollView} from 'react-native-gesture-handler';
import {Edge, SafeAreaView} from 'react-native-safe-area-context' import {Edge, SafeAreaView} from 'react-native-safe-area-context'
type Props = { type Props = {
@@ -109,6 +112,13 @@ function ImageViewing({
return null return null
} }
const [gestureRefs] = useState(new Map())
for (let imageSrc of images) {
if (!gestureRefs.get(imageSrc)) {
gestureRefs.set(imageSrc, createRef())
}
}
return ( return (
<SafeAreaView <SafeAreaView
style={styles.screen} style={styles.screen}
@@ -132,7 +142,6 @@ function ImageViewing({
data={images} data={images}
horizontal horizontal
pagingEnabled pagingEnabled
nestedScrollEnabled
showsHorizontalScrollIndicator={false} showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false} showsVerticalScrollIndicator={false}
getItem={(_, index) => images[index]} getItem={(_, index) => images[index]}
@@ -151,6 +160,13 @@ function ImageViewing({
delayLongPress={delayLongPress} delayLongPress={delayLongPress}
swipeToCloseEnabled={swipeToCloseEnabled} swipeToCloseEnabled={swipeToCloseEnabled}
doubleTapToZoomEnabled={doubleTapToZoomEnabled} doubleTapToZoomEnabled={doubleTapToZoomEnabled}
gestureRef={gestureRefs.get(imageSrc)}
/>
)}
renderScrollComponent={props => (
<ScrollView
{...props}
waitFor={Array.from(gestureRefs.values())}
/> />
)} )}
onMomentumScrollEnd={onScroll} onMomentumScrollEnd={onScroll}