Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [2.2.2](https://github.com/dohooo/react-native-reanimated-carousel/compare/v2.2.1...v2.2.2) (2022-01-23)


### Bug Fixes

* onProgressChange & onSnapToItem bug when only 2 image ([bdb2d74](https://github.com/dohooo/react-native-reanimated-carousel/commit/bdb2d74245dfdefe024ba11b462dbe29bd7e18d6)), closes [#74](https://github.com/dohooo/react-native-reanimated-carousel/issues/74)
* when autoPlay is false, manual sliding will still autoPlay ([6aa3cc4](https://github.com/dohooo/react-native-reanimated-carousel/commit/6aa3cc4a557efaa9e3262202f4a98ff960255b54)), closes [#111](https://github.com/dohooo/react-native-reanimated-carousel/issues/111)

## [2.2.1](https://github.com/dohooo/react-native-reanimated-carousel/compare/v2.1.2...v2.2.1) (2022-01-16)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-reanimated-carousel",
"version": "2.2.1",
"version": "2.2.2",
"description": "Simple carousel component.fully implemented using Reanimated 2.Infinitely scrolling, very smooth.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
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