Skip to content

Commit

Permalink
fix: pinch gesture handler in android
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivanka Todorova committed Feb 2, 2022
1 parent 1c7a5dd commit b8b5b04
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
35 changes: 18 additions & 17 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ export default function App() {
return (
<SafeAreaView style={styles.screen}>
<StatusBar />
<View style={styles.container}>
<PanPinchView
ref={panPinchViewRef}
minScale={1}
initialScale={1}
containerDimensions={{
width: CONTAINER.width,
height: CONTAINER.height,
}}
contentDimensions={{ width: CONTENT.width, height: CONTENT.height }}
>
<Image
style={[styles.image]}
source={require('./assets/photo.jpg')}
/>
</PanPinchView>
</View>
<View style={styles.controls}>
<Button title="Scale to 0.5" onPress={() => scaleTo(0.5)} />
<Button title="Scale to 1.5" onPress={() => scaleTo(1.5)} />
Expand Down Expand Up @@ -70,23 +87,6 @@ export default function App() {
}
/>
</View>
<View style={styles.container}>
<PanPinchView
ref={panPinchViewRef}
minScale={1}
initialScale={1}
containerDimensions={{
width: CONTAINER.width,
height: CONTAINER.height,
}}
contentDimensions={{ width: CONTENT.width, height: CONTENT.height }}
>
<Image
style={[styles.image]}
source={require('./assets/photo.jpg')}
/>
</PanPinchView>
</View>
</SafeAreaView>
);
}
Expand All @@ -108,5 +108,6 @@ const styles = StyleSheet.create({
justifyContent: 'center',
flexDirection: 'row',
flexWrap: 'wrap',
marginVertical: 20,
},
});
23 changes: 19 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { forwardRef, useImperativeHandle } from 'react';
import { StyleSheet, View } from 'react-native';
import { Platform, StyleSheet, View } from 'react-native';
import {
Gesture,
GestureDetector,
Expand Down Expand Up @@ -43,6 +43,8 @@ export default forwardRef(function PanPinchView(
const isPinching = useSharedValue(false);
const isResetting = useSharedValue(false);

const isAndroidPinchActivated = useSharedValue(false);

const contentSize = useVector(
contentDimensions.width,
contentDimensions.height
Expand Down Expand Up @@ -154,7 +156,7 @@ export default forwardRef(function PanPinchView(

const panGesture = Gesture.Pan()
.averageTouches(true)
.onStart(() => {
.onBegin(() => {
'worklet';
onGestureStart();
})
Expand All @@ -173,6 +175,10 @@ export default forwardRef(function PanPinchView(

onGestureStart();

if (Platform.OS === 'android') {
isAndroidPinchActivated.value = false;
}

setAdjustedFocal({ focalX: event.focalX, focalY: event.focalY });
origin.x.value = adjustedFocal.x.value;
origin.y.value = adjustedFocal.y.value;
Expand All @@ -183,10 +189,19 @@ export default forwardRef(function PanPinchView(
.onChange((event) => {
'worklet';

if (event.numberOfPointers < 2) {
if (event.numberOfPointers !== 2) {
return;
}

if (!isAndroidPinchActivated.value && Platform.OS === 'android') {
setAdjustedFocal({ focalX: event.focalX, focalY: event.focalY });

origin.x.value = adjustedFocal.x.value;
origin.y.value = adjustedFocal.y.value;

isAndroidPinchActivated.value = true;
}

isPinching.value = true;
scale.value = Math.max(scale.value * event.scaleChange, minScale);

Expand Down Expand Up @@ -252,7 +267,7 @@ export default forwardRef(function PanPinchView(
}
);

const gestures = Gesture.Race(panGesture, pinchGesture);
const gestures = Gesture.Simultaneous(panGesture, pinchGesture);

return (
<GestureHandlerRootView>
Expand Down

0 comments on commit b8b5b04

Please sign in to comment.