Skip to content

Commit

Permalink
fix: autoplay will paused when click
Browse files Browse the repository at this point in the history
fix #115
  • Loading branch information
dohooo committed Jan 24, 2022
1 parent ef211e5 commit eb21293
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
16 changes: 11 additions & 5 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,25 @@ function Carousel<T>(
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);
Expand Down Expand Up @@ -186,6 +190,8 @@ function Carousel<T>(
translation={handlerOffsetX}
onScrollBegin={scrollViewGestureOnScrollBegin}
onScrollEnd={scrollViewGestureOnScrollEnd}
onTouchBegin={scrollViewGestureOnTouchBegin}
onTouchEnd={scrollViewGestureOnTouchEnd}
>
<Animated.View
key={mode}
Expand Down
71 changes: 49 additions & 22 deletions src/ScrollViewGesture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type GestureContext = {
interface Props {
size: number;
infinite?: boolean;
onScrollEnd?: () => void;
onScrollBegin?: () => void;
onScrollEnd?: () => void;
onTouchBegin?: () => void;
onTouchEnd?: () => void;
style?: StyleProp<ViewStyle>;
translation: Animated.SharedValue<number>;
}
Expand All @@ -45,7 +47,14 @@ const IScrollViewGesture: React.FC<Props> = (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]);
Expand Down Expand Up @@ -116,22 +125,28 @@ const IScrollViewGesture: React.FC<Props> = (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;
}
Expand All @@ -158,15 +173,14 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
}
}
}, [
infinite,
touching,
_withSpring,
touching.value,
translation,
scrollEndTranslation,
scrollEndVelocity,
onScrollEnd,
maxPage,
size,
scrollEndTranslation.value,
infinite,
activeDecay,
_withSpring,
]);

useAnimatedReaction(
Expand All @@ -176,7 +190,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
resetBoundary();
}
},
[pagingEnabled]
[pagingEnabled, resetBoundary]
);

const panGestureEventHandler = useAnimatedGestureHandler<
Expand Down Expand Up @@ -227,15 +241,28 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
}
},
},
[pagingEnabled, isHorizontal.value, infinite, maxPage, size, enableSnap]
[
pagingEnabled,
isHorizontal.value,
infinite,
maxPage,
size,
enableSnap,
onScrollBegin,
onScrollEnd,
]
);

const directionStyle = React.useMemo(() => {
return vertical ? styles.contentHorizontal : styles.contentVertical;
}, [vertical]);

return (
<Animated.View style={[styles.container, directionStyle, style]}>
<Animated.View
style={[styles.container, directionStyle, style]}
onTouchStart={onTouchBegin}
onTouchEnd={onTouchEnd}
>
<PanGestureHandler
{...panGestureHandlerProps}
onGestureEvent={panGestureEventHandler}
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useAutoPlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ export function useAutoPlay(opts: {
}, [autoPlayReverse, autoPlayInterval, carouselController]);

const pause = React.useCallback(() => {
if (!autoPlay) {
return;
}
timer.current && clearInterval(timer.current);
stopped.current = true;
}, []);
}, [autoPlay]);

const start = React.useCallback(() => {
if (!autoPlay) {
Expand Down

0 comments on commit eb21293

Please sign in to comment.