Skip to content

Commit

Permalink
fix(bug)!: fix jump at the end of pinch gesture
Browse files Browse the repository at this point in the history
Disable pan gesture at the beginning of pinch gesture instead of the end
this change has been applied to both ResumableZoom and CropZoom

refs #10
  • Loading branch information
Glazzes committed Mar 27, 2024
1 parent f692eb6 commit 948783f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/commons/hooks/usePinchCommons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
runOnJS,
type SharedValue,
} from 'react-native-reanimated';
import type {
GestureUpdateEvent,
PinchGestureHandlerEventPayload,
import {
type GestureUpdateEvent,
type PinchGestureHandlerEventPayload,
} from 'react-native-gesture-handler';

import { clamp } from '../utils/clamp';
Expand Down Expand Up @@ -69,9 +69,9 @@ export const usePinchCommons = (options: PinchOptions) => {
} = options;

const [gesturesEnabled, setGesturesEnabled] = useState<boolean>(true);
const toggleGestures = () => {
const switchGesturesState = (value: boolean) => {
if (scaleMode === ScaleMode.BOUNCE) {
setGesturesEnabled((prev) => !prev);
setGesturesEnabled(value);
}
};

Expand All @@ -92,12 +92,13 @@ export const usePinchCommons = (options: PinchOptions) => {
translate.x.value = withTiming(toX);
translate.y.value = withTiming(toY);
scale.value = withTiming(toScale, undefined, () => {
runOnJS(toggleGestures)();
runOnJS(switchGesturesState)(true);
});
};

const onPinchStart = (e: PinchGestureEvent) => {
'worklet';
runOnJS(switchGesturesState)(false);

cancelAnimation(translate.x);
cancelAnimation(translate.y);
Expand All @@ -120,6 +121,9 @@ export const usePinchCommons = (options: PinchOptions) => {

const onPinchUpdate = (e: PinchGestueUpdateEvent) => {
'worklet';
if (e.numberOfPointers !== 2) {
return;
}

let toScale = e.scale * scaleOffset.value;
if (scaleMode === ScaleMode.CLAMP) {
Expand Down Expand Up @@ -151,8 +155,6 @@ export const usePinchCommons = (options: PinchOptions) => {
const onPinchEnd = (e: PinchGestureEvent) => {
'worklet';

runOnJS(toggleGestures)();

if (userCallbacks?.onPinchEnd) {
runOnJS(userCallbacks.onPinchEnd)(e);
}
Expand Down
1 change: 0 additions & 1 deletion src/components/crop/CropZoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ const CropZoom: React.FC<CropZoomProps> = (props) => {
});

const pinch = Gesture.Pinch()
.enabled(gesturesEnabled)
.onStart(onPinchStart)
.onUpdate(onPinchUpdate)
.onEnd(onPinchEnd);
Expand Down
2 changes: 1 addition & 1 deletion src/components/resumable/ResumableZoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const ResumableZoom: React.FC<ResumableZoomProps> = (props) => {
});

const pinch = Gesture.Pinch()
.enabled(pinchEnabled && gesturesEnabled)
.enabled(pinchEnabled)
.hitSlop(hitSlop)
.onStart(onPinchStart)
.onUpdate(onPinchUpdate)
Expand Down

0 comments on commit 948783f

Please sign in to comment.