This commit is contained in:
Dan Abramov
2024-11-12 23:04:09 +00:00
parent 002ac6aba9
commit f4479fdb26
@@ -88,10 +88,6 @@ const ImageItem = ({
: 1, : 1,
) )
const needsReset = useSharedValue(false)
const lastValue = useSharedValue(null)
const weirdState = useSharedValue(0)
function computeInsets(zoom) { function computeInsets(zoom) {
'worklet' 'worklet'
const screenSize = measureSafeArea() const screenSize = measureSafeArea()
@@ -107,90 +103,36 @@ const ImageItem = ({
const finalZoomScale = screenSize.height / renderedHeight const finalZoomScale = screenSize.height / renderedHeight
const currentZoomScale = zoom const currentZoomScale = zoom
console.log(currentZoomScale, finalZoomScale)
top = top =
interpolate( interpolate(
currentZoomScale, currentZoomScale,
[1, finalZoomScale], [1, finalZoomScale],
[horizontalBarHeight, 0], [0, horizontalBarHeight],
) / currentZoomScale ) * zoom
bottom = top
bottom = renderedHeight - top
} }
} }
return {top, bottom, left: 0, right: 0} return {top, bottom, left: 0, right: 0}
} }
const insets = useSharedValue(computeInsets(1))
const animatedProps = useAnimatedProps(() => {
console.log('setting', insets.value)
return {
contentInsets: insets.value,
}
})
const scrollHandler = useAnimatedScrollHandler({ const scrollHandler = useAnimatedScrollHandler({
onScroll(e) { onScroll(e) {
console.log('scroll')
if (
lastValue.value &&
e.contentOffset.x === lastValue.value.contentOffset.x &&
e.contentOffset.y === lastValue.value.contentOffset.y &&
e.zoomScale === lastValue.value.zoomScale &&
weirdState.value === 1
) {
weirdState.value = 2
} else {
weirdState.value = 0
}
lastValue.value = e
// console.log('inside condition, set did nothing = true')
// // setNativeProps(scrollViewRef, {
// // scrollEnabled: false,
// // })
// // setNativeProps(scrollViewRef, {
// // scrollEnabled: true,
// // })
// } else {
// console.log('set did nothing = false')
// didNothing.value = false
// }
const nextIsScaled = e.zoomScale > 1 const nextIsScaled = e.zoomScale > 1
if (scaled !== nextIsScaled) { if (scaled !== nextIsScaled) {
runOnJS(handleZoom)(nextIsScaled) runOnJS(handleZoom)(nextIsScaled)
} }
},
onBeginDrag() {
console.log('beg drag')
if (needsReset.value) {
console.log('reset!!')
needsReset.value = false
setNativeProps(scrollViewRef, {
scrollEnabled: false,
})
setNativeProps(scrollViewRef, {
scrollEnabled: true,
})
}
},
onEndDrag(e) {
console.log('end drag')
insets.value = computeInsets(e.zoomScale) const insets = computeInsets(e.zoomScale)
}, console.log(e.contentOffset.y, insets.top, insets.bottom)
onMomentumBegin() {
console.log('beg mom') setNativeProps(scrollViewRef, {})
weirdState.value = 1 // const insets = computeInsets(e.zoomScale)
}, // setNativeProps(scrollViewRef, {
onMomentumEnd() { // contentInset: insets,
console.log('mom end') // })
if (weirdState.value === 2) {
weirdState.value = 3
}
}, },
onEndDrag(e) {},
}) })
function handleZoom(nextIsScaled: boolean) { function handleZoom(nextIsScaled: boolean) {
@@ -212,33 +154,6 @@ const ImageItem = ({
}) })
} }
const manual = Gesture.Manual()
.onTouchesDown(() => {
console.log(' -- touch down')
if (weirdState.value === 3) {
weirdState.value = 0
console.log('-- set needs reset')
needsReset.value = true
// setTimeout(() => {
// setNativeProps(scrollViewRef, {
// scrollEnabled: true,
// })
// }, 100)
}
})
.onTouchesUp(() => {
console.log(' -- touch up')
// setNativeProps(scrollViewRef, {
// scrollEnabled: true,
// })
})
.onTouchesCancelled(() => {
// setNativeProps(scrollViewRef, {
// scrollEnabled: true,
// })
console.log(' -- touch can')
})
const singleTap = Gesture.Tap().onEnd(() => { const singleTap = Gesture.Tap().onEnd(() => {
'worklet' 'worklet'
runOnJS(onTap)() runOnJS(onTap)()
@@ -268,17 +183,19 @@ const ImageItem = ({
runOnJS(zoomTo)(nextZoomRect) runOnJS(zoomTo)(nextZoomRect)
}) })
const composedGesture = Gesture.Simultaneous( const composedGesture = Gesture.Exclusive(
manual, dismissSwipePan,
Gesture.Exclusive(dismissSwipePan, doubleTap, singleTap), doubleTap,
singleTap,
) )
const containerStyle = useAnimatedStyle(() => { const containerStyle = useAnimatedStyle(() => {
const {scaleAndMoveTransform, isHidden} = transforms.value const {scaleAndMoveTransform, isHidden} = transforms.value
return { return {
flex: 1, // flex: 1,
transform: scaleAndMoveTransform, transform: scaleAndMoveTransform,
opacity: isHidden ? 0 : 1, opacity: isHidden ? 0 : 1,
backgroundColor: 'red',
} }
}) })
@@ -336,9 +253,9 @@ const ImageItem = ({
maximumZoomScale={maxZoomScale} maximumZoomScale={maxZoomScale}
onScroll={scrollHandler} onScroll={scrollHandler}
style={containerStyle} style={containerStyle}
bounces={scaled} contentContainerStyle={{
bouncesZoom={true} flex: 1,
animatedProps={animatedProps}> }}>
{showLoader && ( {showLoader && (
<ActivityIndicator size="small" color="#FFF" style={styles.loading} /> <ActivityIndicator size="small" color="#FFF" style={styles.loading} />
)} )}