Skip to content

Commit eb21293

Browse files
committed
fix: autoplay will paused when click
fix #115
1 parent ef211e5 commit eb21293

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

src/Carousel.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,25 @@ function Carousel<T>(
9090
carouselController,
9191
});
9292

93-
const scrollViewGestureOnScrollBegin = React.useCallback(() => {
94-
pause();
95-
onScrollBegin?.();
96-
}, [onScrollBegin, pause]);
97-
9893
const _onScrollEnd = React.useCallback(() => {
9994
computedIndex();
10095
onScrollEnd?.(sharedPreIndex.current, sharedIndex.current);
10196
}, [sharedPreIndex, sharedIndex, computedIndex, onScrollEnd]);
10297

98+
const scrollViewGestureOnScrollBegin = React.useCallback(() => {
99+
pause();
100+
onScrollBegin?.();
101+
}, [onScrollBegin, pause]);
102+
103103
const scrollViewGestureOnScrollEnd = React.useCallback(() => {
104104
start();
105105
_onScrollEnd();
106106
}, [_onScrollEnd, start]);
107107

108+
const scrollViewGestureOnTouchBegin = React.useCallback(pause, [pause]);
109+
110+
const scrollViewGestureOnTouchEnd = React.useCallback(start, [start]);
111+
108112
const goToIndex = React.useCallback(
109113
(i: number, animated?: boolean) => {
110114
carouselController.to(i, animated);
@@ -186,6 +190,8 @@ function Carousel<T>(
186190
translation={handlerOffsetX}
187191
onScrollBegin={scrollViewGestureOnScrollBegin}
188192
onScrollEnd={scrollViewGestureOnScrollEnd}
193+
onTouchBegin={scrollViewGestureOnTouchBegin}
194+
onTouchEnd={scrollViewGestureOnTouchEnd}
189195
>
190196
<Animated.View
191197
key={mode}

src/ScrollViewGesture.tsx

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ type GestureContext = {
2525
interface Props {
2626
size: number;
2727
infinite?: boolean;
28-
onScrollEnd?: () => void;
2928
onScrollBegin?: () => void;
29+
onScrollEnd?: () => void;
30+
onTouchBegin?: () => void;
31+
onTouchEnd?: () => void;
3032
style?: StyleProp<ViewStyle>;
3133
translation: Animated.SharedValue<number>;
3234
}
@@ -45,7 +47,14 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
4547
},
4648
} = React.useContext(CTX);
4749

48-
const { translation, onScrollBegin, onScrollEnd, size } = props;
50+
const {
51+
translation,
52+
size,
53+
onScrollBegin,
54+
onScrollEnd,
55+
onTouchBegin,
56+
onTouchEnd,
57+
} = props;
4958

5059
const maxPage = data.length;
5160
const isHorizontal = useDerivedValue(() => !vertical, [vertical]);
@@ -116,22 +125,28 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
116125
]
117126
);
118127

119-
const resetBoundary = React.useCallback(() => {
120-
'worklet';
121-
const onFinish = (isFinished: boolean) => {
128+
const onFinish = React.useCallback(
129+
(isFinished: boolean) => {
130+
'worklet';
122131
if (isFinished) {
123132
touching.value = false;
124133
onScrollEnd && runOnJS(onScrollEnd)();
125134
}
126-
};
127-
const activeDecay = () => {
128-
touching.value = true;
129-
translation.value = withDecay(
130-
{ velocity: scrollEndVelocity.value },
131-
onFinish
132-
);
133-
};
135+
},
136+
[onScrollEnd, touching]
137+
);
138+
139+
const activeDecay = React.useCallback(() => {
140+
'worklet';
141+
touching.value = true;
142+
translation.value = withDecay(
143+
{ velocity: scrollEndVelocity.value },
144+
onFinish
145+
);
146+
}, [onFinish, scrollEndVelocity.value, touching, translation]);
134147

148+
const resetBoundary = React.useCallback(() => {
149+
'worklet';
135150
if (touching.value) {
136151
return;
137152
}
@@ -158,15 +173,14 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
158173
}
159174
}
160175
}, [
161-
infinite,
162-
touching,
163-
_withSpring,
176+
touching.value,
164177
translation,
165-
scrollEndTranslation,
166-
scrollEndVelocity,
167-
onScrollEnd,
168178
maxPage,
169179
size,
180+
scrollEndTranslation.value,
181+
infinite,
182+
activeDecay,
183+
_withSpring,
170184
]);
171185

172186
useAnimatedReaction(
@@ -176,7 +190,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
176190
resetBoundary();
177191
}
178192
},
179-
[pagingEnabled]
193+
[pagingEnabled, resetBoundary]
180194
);
181195

182196
const panGestureEventHandler = useAnimatedGestureHandler<
@@ -227,15 +241,28 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
227241
}
228242
},
229243
},
230-
[pagingEnabled, isHorizontal.value, infinite, maxPage, size, enableSnap]
244+
[
245+
pagingEnabled,
246+
isHorizontal.value,
247+
infinite,
248+
maxPage,
249+
size,
250+
enableSnap,
251+
onScrollBegin,
252+
onScrollEnd,
253+
]
231254
);
232255

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

237260
return (
238-
<Animated.View style={[styles.container, directionStyle, style]}>
261+
<Animated.View
262+
style={[styles.container, directionStyle, style]}
263+
onTouchStart={onTouchBegin}
264+
onTouchEnd={onTouchEnd}
265+
>
239266
<PanGestureHandler
240267
{...panGestureHandlerProps}
241268
onGestureEvent={panGestureEventHandler}

src/hooks/useAutoPlay.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ export function useAutoPlay(opts: {
3030
}, [autoPlayReverse, autoPlayInterval, carouselController]);
3131

3232
const pause = React.useCallback(() => {
33+
if (!autoPlay) {
34+
return;
35+
}
3336
timer.current && clearInterval(timer.current);
3437
stopped.current = true;
35-
}, []);
38+
}, [autoPlay]);
3639

3740
const start = React.useCallback(() => {
3841
if (!autoPlay) {

0 commit comments

Comments
 (0)