From eb21293d6c0544da9e62a418505945dc46a59cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=9D=B1=E6=BE=94?= Date: Mon, 24 Jan 2022 14:30:44 +0800 Subject: [PATCH] fix: autoplay will paused when click fix #115 --- src/Carousel.tsx | 16 ++++++--- src/ScrollViewGesture.tsx | 71 +++++++++++++++++++++++++++------------ src/hooks/useAutoPlay.ts | 5 ++- 3 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/Carousel.tsx b/src/Carousel.tsx index b4dc1ead..c792d4ea 100644 --- a/src/Carousel.tsx +++ b/src/Carousel.tsx @@ -90,21 +90,25 @@ function Carousel( carouselController, }); - const scrollViewGestureOnScrollBegin = React.useCallback(() => { - pause(); - onScrollBegin?.(); - }, [onScrollBegin, pause]); - const _onScrollEnd = React.useCallback(() => { computedIndex(); onScrollEnd?.(sharedPreIndex.current, sharedIndex.current); }, [sharedPreIndex, sharedIndex, computedIndex, onScrollEnd]); + const scrollViewGestureOnScrollBegin = React.useCallback(() => { + pause(); + onScrollBegin?.(); + }, [onScrollBegin, pause]); + const scrollViewGestureOnScrollEnd = React.useCallback(() => { start(); _onScrollEnd(); }, [_onScrollEnd, start]); + const scrollViewGestureOnTouchBegin = React.useCallback(pause, [pause]); + + const scrollViewGestureOnTouchEnd = React.useCallback(start, [start]); + const goToIndex = React.useCallback( (i: number, animated?: boolean) => { carouselController.to(i, animated); @@ -186,6 +190,8 @@ function Carousel( translation={handlerOffsetX} onScrollBegin={scrollViewGestureOnScrollBegin} onScrollEnd={scrollViewGestureOnScrollEnd} + onTouchBegin={scrollViewGestureOnTouchBegin} + onTouchEnd={scrollViewGestureOnTouchEnd} > void; onScrollBegin?: () => void; + onScrollEnd?: () => void; + onTouchBegin?: () => void; + onTouchEnd?: () => void; style?: StyleProp; translation: Animated.SharedValue; } @@ -45,7 +47,14 @@ const IScrollViewGesture: React.FC = (props) => { }, } = React.useContext(CTX); - const { translation, onScrollBegin, onScrollEnd, size } = props; + const { + translation, + size, + onScrollBegin, + onScrollEnd, + onTouchBegin, + onTouchEnd, + } = props; const maxPage = data.length; const isHorizontal = useDerivedValue(() => !vertical, [vertical]); @@ -116,22 +125,28 @@ const IScrollViewGesture: React.FC = (props) => { ] ); - const resetBoundary = React.useCallback(() => { - 'worklet'; - const onFinish = (isFinished: boolean) => { + const onFinish = React.useCallback( + (isFinished: boolean) => { + 'worklet'; if (isFinished) { touching.value = false; onScrollEnd && runOnJS(onScrollEnd)(); } - }; - const activeDecay = () => { - touching.value = true; - translation.value = withDecay( - { velocity: scrollEndVelocity.value }, - onFinish - ); - }; + }, + [onScrollEnd, touching] + ); + + const activeDecay = React.useCallback(() => { + 'worklet'; + touching.value = true; + translation.value = withDecay( + { velocity: scrollEndVelocity.value }, + onFinish + ); + }, [onFinish, scrollEndVelocity.value, touching, translation]); + const resetBoundary = React.useCallback(() => { + 'worklet'; if (touching.value) { return; } @@ -158,15 +173,14 @@ const IScrollViewGesture: React.FC = (props) => { } } }, [ - infinite, - touching, - _withSpring, + touching.value, translation, - scrollEndTranslation, - scrollEndVelocity, - onScrollEnd, maxPage, size, + scrollEndTranslation.value, + infinite, + activeDecay, + _withSpring, ]); useAnimatedReaction( @@ -176,7 +190,7 @@ const IScrollViewGesture: React.FC = (props) => { resetBoundary(); } }, - [pagingEnabled] + [pagingEnabled, resetBoundary] ); const panGestureEventHandler = useAnimatedGestureHandler< @@ -227,7 +241,16 @@ const IScrollViewGesture: React.FC = (props) => { } }, }, - [pagingEnabled, isHorizontal.value, infinite, maxPage, size, enableSnap] + [ + pagingEnabled, + isHorizontal.value, + infinite, + maxPage, + size, + enableSnap, + onScrollBegin, + onScrollEnd, + ] ); const directionStyle = React.useMemo(() => { @@ -235,7 +258,11 @@ const IScrollViewGesture: React.FC = (props) => { }, [vertical]); return ( - + { + if (!autoPlay) { + return; + } timer.current && clearInterval(timer.current); stopped.current = true; - }, []); + }, [autoPlay]); const start = React.useCallback(() => { if (!autoPlay) {