Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/violet-chicken-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-native-reanimated-carousel': patch
---

improve "slow pan" behavior: if it seems that the user intent is to stay on the current page (because they didn't pan very far; maybe they started panning one direction then reversed direction, etc.), _don't_ actually change page upon gesture completion
20 changes: 17 additions & 3 deletions src/components/ScrollViewGesture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,28 @@ const IScrollViewGesture: React.FC<PropsWithChildren<Props>> = (props) => {
*
* `page size` equals to `size` variable.
* */

// calculate target "nextPage" based on the final pan position and the velocity of
// the pan gesture at termination; this allows for a quick "flick" to indicate a far
// off page change.
const nextPage = -Math.round((origin + velocity * 2) / size);

if (pagingEnabled) {
// we'll never go further than a single page away from the current page when paging
// is enabled.

// distance with direction
const offset = -(scrollEndTranslation.value >= 0 ? 1 : -1); // 1 or -1
const computed = offset < 0 ? Math.ceil : Math.floor;
const page = computed(-translation.value / size);

if (loop) {
const velocityDirection = -Math.sign(velocity);
if (page === nextPage || velocityDirection !== offset) {
// not going anywhere! Velocity was insufficient to overcome the distance to get to a
// further page. Let's reset gently to the current page.
finalTranslation = withSpring(withProcessTranslation(-page * size), onFinished);
}
else if (loop) {
const finalPage = page + offset;
finalTranslation = withSpring(withProcessTranslation(-finalPage * size), onFinished);
}
Expand All @@ -153,8 +168,7 @@ const IScrollViewGesture: React.FC<PropsWithChildren<Props>> = (props) => {

if (!pagingEnabled && snapEnabled) {
// scroll to the nearest item
const nextPage = Math.round((origin + velocity * 0.4) / size) * size;
finalTranslation = withSpring(withProcessTranslation(nextPage), onFinished);
finalTranslation = withSpring(withProcessTranslation(-nextPage * size), onFinished);
}
}

Expand Down