Skip to content

Commit 3d9b62a

Browse files
committed
refactor: scroll animation duration of ScrollViewGesture
#101
1 parent c69d6b6 commit 3d9b62a

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

example/src/anim-tab-bar/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ function Index() {
2424
defaultValue: false,
2525
buttonTitle: ElementsText.AUTOPLAY,
2626
});
27+
const [loop, setLoop] = React.useState(false);
2728

2829
return (
2930
<View style={{ flex: 1 }}>
3031
<View style={{ marginVertical: 100 }}>
3132
<Carousel
33+
key={`${loop}`}
3234
ref={r}
33-
loop={false}
35+
loop={loop}
3436
style={{
3537
width: window.width,
3638
height: PAGE_HEIGHT,
@@ -48,10 +50,10 @@ function Index() {
4850
animationValue={animationValue}
4951
label={item}
5052
onPress={() =>
51-
r.current?.scrollTo(
52-
animationValue.value,
53-
true
54-
)
53+
r.current?.scrollTo({
54+
count: animationValue.value,
55+
animated: true,
56+
})
5557
}
5658
/>
5759
);
@@ -60,6 +62,7 @@ function Index() {
6062
/>
6163
</View>
6264
{AutoPLay.button}
65+
<SButton onPress={() => setLoop(!loop)}>{`Loop: ${loop}`}</SButton>
6366
<View
6467
style={{
6568
marginTop: 24,

example/src/hooks/useToggleButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function useToggleButton(opts: {
1111
const button = React.useMemo(() => {
1212
return (
1313
<SButton onPress={() => setStatus(!status)}>
14-
{buttonTitle}:{`${status}`}
14+
{buttonTitle}: {`${status}`}
1515
</SButton>
1616
);
1717
}, [status, buttonTitle]);

example/src/normal/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function Index() {
3030
{...baseOptions}
3131
loop
3232
autoPlay={isAutoPlay}
33-
scrollAnimationDuration={2000}
3433
autoPlayInterval={isFast ? 100 : 2000}
3534
data={[...new Array(6).keys()]}
3635
renderItem={({ index }) => <SBItem key={index} index={index} />}

src/Carousel.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ function Carousel<T>(
8484

8585
const { run, pause } = useAutoPlay({
8686
autoPlay,
87-
autoPlayInterval: Math.max(
88-
autoPlayInterval - scrollAnimationDuration,
89-
0
90-
),
87+
autoPlayInterval: Math.max(autoPlayInterval, 0),
9188
autoPlayReverse,
9289
carouselController,
9390
});

src/ScrollViewGesture.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
4141
enableSnap,
4242
panGestureHandlerProps,
4343
loop: infinite,
44-
autoPlayInterval,
44+
scrollAnimationDuration,
4545
},
4646
} = React.useContext(CTX);
4747

@@ -59,7 +59,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
5959
return withTiming(
6060
toValue,
6161
{
62-
duration: autoPlayInterval,
62+
duration: scrollAnimationDuration,
6363
easing: Easing.easeOutQuart,
6464
},
6565
(isFinished) => {
@@ -69,7 +69,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
6969
}
7070
);
7171
},
72-
[autoPlayInterval]
72+
[scrollAnimationDuration]
7373
);
7474

7575
const endWithSpring = React.useCallback(

src/hooks/useAutoPlay.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export function useAutoPlay(opts: {
2929

3030
timer.current = setTimeout(() => {
3131
autoPlayReverse
32-
? carouselController.prev(run)
33-
: carouselController.next(run);
32+
? carouselController.prev({ onFinished: run })
33+
: carouselController.next({ onFinished: run });
3434
}, autoPlayInterval);
3535
}, [autoPlayReverse, autoPlayInterval, carouselController]);
3636

src/hooks/useCarouselController.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import React from 'react';
22
import type Animated from 'react-native-reanimated';
33
import { Easing } from '../constants';
44
import { runOnJS, useSharedValue, withTiming } from 'react-native-reanimated';
5-
6-
interface TCarouselActionOptions {
7-
count?: number;
8-
animated?: boolean;
9-
onFinished?: () => void;
10-
}
5+
import type { TCarouselActionOptions } from '../types';
116

127
interface IOpts {
138
loop: boolean;

src/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ export interface ICarouselInstance {
146146
* Scroll to previous item, it takes one optional argument (count),
147147
* which allows you to specify how many items to cross
148148
*/
149-
prev: (count?: number) => void;
149+
prev: (opts?: TCarouselActionOptions) => void;
150150
/**
151151
* Scroll to next item, it takes one optional argument (count),
152152
* which allows you to specify how many items to cross
153153
*/
154-
next: (count?: number) => void;
154+
next: (opts?: TCarouselActionOptions) => void;
155155
/**
156156
* Get current item index
157157
*/
@@ -165,7 +165,7 @@ export interface ICarouselInstance {
165165
* Use value to scroll to a position where relative to the current position,
166166
* scrollTo(-2) is equivalent to prev(2), scrollTo(2) is equivalent to next(2)
167167
*/
168-
scrollTo: (value: number, animated?: boolean) => void;
168+
scrollTo: (opts?: TCarouselActionOptions) => void;
169169
}
170170

171171
export interface CarouselRenderItemInfo<ItemT> {
@@ -177,3 +177,9 @@ export interface CarouselRenderItemInfo<ItemT> {
177177
export type CarouselRenderItem<ItemT> = (
178178
info: CarouselRenderItemInfo<ItemT>
179179
) => React.ReactElement;
180+
181+
export interface TCarouselActionOptions {
182+
count?: number;
183+
animated?: boolean;
184+
onFinished?: () => void;
185+
}

0 commit comments

Comments
 (0)