Skip to content

Commit

Permalink
fix(bug)!: fix jumps issue at the end of pan gesture
Browse files Browse the repository at this point in the history
refs #10
  • Loading branch information
Glazzes authored Mar 28, 2024
2 parents f692eb6 + 8422651 commit 0793748
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 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
5 changes: 2 additions & 3 deletions src/components/crop/CropZoom.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useImperativeHandle } from 'react';
import { StyleSheet, View, type ViewStyle } from 'react-native';
import { Platform, StyleSheet, View, type ViewStyle } from 'react-native';
import Animated, {
useAnimatedStyle,
useDerivedValue,
Expand Down Expand Up @@ -42,7 +42,7 @@ const CropZoom: React.FC<CropZoomProps> = (props) => {
maxScale: userMaxScale = -1,
scaleMode = ScaleMode.BOUNCE,
panMode = PanMode.FREE,
panWithPinch = true,
panWithPinch = Platform.OS !== 'ios',
mode = CropMode.MANAGED,
onGestureActive = undefined,
OverlayComponent = undefined,
Expand Down 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
11 changes: 8 additions & 3 deletions src/components/resumable/ResumableZoom.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React, { useImperativeHandle } from 'react';
import { View, StyleSheet, type LayoutChangeEvent } from 'react-native';
import {
Platform,
View,
StyleSheet,
type LayoutChangeEvent,
} from 'react-native';
import Animated, {
useAnimatedStyle,
useDerivedValue,
Expand Down Expand Up @@ -41,7 +46,7 @@ const ResumableZoom: React.FC<ResumableZoomProps> = (props) => {
maxScale: userMaxScale = 6,
panMode = PanMode.CLAMP,
scaleMode = ScaleMode.BOUNCE,
panWithPinch = true,
panWithPinch = Platform.OS !== 'ios',
onTap,
onGestureActive,
onSwipeRight,
Expand Down Expand Up @@ -160,7 +165,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 0793748

Please sign in to comment.