Skip to content

Commit

Permalink
Merge pull request #64 from dohooo/fix63
Browse files Browse the repository at this point in the history
fix: scroll pass the last item
  • Loading branch information
dohooo authored Dec 28, 2021
2 parents a54a825 + 34a5e57 commit 47edcaf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function Carousel<T>(
infinite={loop}
translation={handlerOffsetX}
style={style}
max={data.length * size}
maxPage={data.length}
size={size}
panGestureHandlerProps={panGestureHandlerProps}
onScrollBegin={scrollViewGestureOnScrollBegin}
Expand Down
18 changes: 9 additions & 9 deletions src/ScrollViewGesture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface Props {

translation: Animated.SharedValue<number>;
size: number;
max: number;
maxPage: number;
}

const IScrollViewGesture: React.FC<Props> = (props) => {
Expand All @@ -49,7 +49,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
onScrollBegin,
onScrollEnd,
size,
max,
maxPage,
} = props;

const isHorizontal = useDerivedValue(() => !vertical, [vertical]);
Expand Down Expand Up @@ -87,11 +87,11 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
Math.max(page - 1, velocityPage)
);
if (!infinite) {
finalPage = Math.min(max - 1, Math.max(0, finalPage));
finalPage = Math.min(maxPage - 1, Math.max(0, finalPage));
}
translation.value = endWithSpring(-finalPage * size, onFinished);
},
[infinite, endWithSpring, translation, scrollEndVelocity, size, max]
[infinite, endWithSpring, translation, scrollEndVelocity, size, maxPage]
);

const resetBoundary = React.useCallback(() => {
Expand Down Expand Up @@ -125,13 +125,13 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
}
}

if (translation.value < -(max - size)) {
if (translation.value < -((maxPage - 1) * size)) {
if (scrollEndTranslation.value > 0) {
activeDecay();
return;
}
if (!infinite) {
translation.value = endWithSpring(-(max - size));
translation.value = endWithSpring(-((maxPage - 1) * size));
return;
}
}
Expand All @@ -143,7 +143,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
scrollEndTranslation,
scrollEndVelocity,
onScrollEnd,
max,
maxPage,
size,
]);

Expand All @@ -166,7 +166,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
touching.value = true;
cancelAnimation(translation);
onScrollBegin && runOnJS(onScrollBegin)();
ctx.max = max - size;
ctx.max = (maxPage - 1) * size;
ctx.panOffset = translation.value;
},
onActive: (e, ctx) => {
Expand Down Expand Up @@ -214,7 +214,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
}
},
},
[pagingEnabled, isHorizontal.value, infinite, max, size]
[pagingEnabled, isHorizontal.value, infinite, maxPage, size]
);

const directionStyle = React.useMemo(() => {
Expand Down

0 comments on commit 47edcaf

Please sign in to comment.