Skip to content

Commit 3c9f5ba

Browse files
committed
simplified pan in v3 by using event.changeX
1 parent 6c663b2 commit 3c9f5ba

7 files changed

Lines changed: 15 additions & 36 deletions

File tree

packages/docs-gesture-handler/static/examples/FlingGestureBasic.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import {
77
} from 'react-native-gesture-handler';
88
import { StyleSheet, View } from 'react-native';
99
import Animated, {
10+
clamp,
1011
withTiming,
1112
useSharedValue,
1213
useAnimatedStyle,
1314
} from 'react-native-reanimated';
1415

15-
function clamp(val, min, max) {
16-
return Math.min(Math.max(val, min), max);
17-
}
18-
1916
export default function App() {
2017
const translateX = useSharedValue(0);
2118
const startTranslateX = useSharedValue(0);

packages/docs-gesture-handler/static/examples/PanGestureBasic.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import Animated, {
3+
clamp,
34
useSharedValue,
45
useAnimatedStyle,
56
} from 'react-native-reanimated';
@@ -10,15 +11,9 @@ import {
1011
} from 'react-native-gesture-handler';
1112
import { StyleSheet, View } from 'react-native';
1213

13-
function clamp(val, min, max) {
14-
return Math.min(Math.max(val, min), max);
15-
}
16-
1714
export default function App() {
1815
const translationX = useSharedValue(0);
1916
const translationY = useSharedValue(0);
20-
const prevTranslationX = useSharedValue(0);
21-
const prevTranslationY = useSharedValue(0);
2217
const grabbing = useSharedValue(false);
2318
const maxTranslateX = useSharedValue(0);
2419
const maxTranslateY = useSharedValue(0);
@@ -60,17 +55,15 @@ export default function App() {
6055
minDistance: 1,
6156
onBegin: () => {
6257
grabbing.value = true;
63-
prevTranslationX.value = translationX.value;
64-
prevTranslationY.value = translationY.value;
6558
},
6659
onUpdate: (event) => {
6760
translationX.value = clamp(
68-
prevTranslationX.value + event.translationX,
61+
translationX.value + event.changeX,
6962
-maxTranslateX.value,
7063
maxTranslateX.value
7164
);
7265
translationY.value = clamp(
73-
prevTranslationY.value + event.translationY,
66+
translationX.value + event.changeY,
7467
-maxTranslateY.value,
7568
maxTranslateY.value
7669
);
@@ -81,8 +74,8 @@ export default function App() {
8174
});
8275

8376
return (
84-
<GestureHandlerRootView >
85-
<View ref={containerRef} style={styles.container} >
77+
<GestureHandlerRootView>
78+
<View ref={containerRef} style={styles.container}>
8679
<GestureDetector gesture={pan}>
8780
<Animated.View style={[animatedStyles, styles.box]}></Animated.View>
8881
</GestureDetector>

packages/docs-gesture-handler/static/examples/PinchGestureBasic.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ import {
66
} from 'react-native-gesture-handler';
77
import { StyleSheet, View } from 'react-native';
88
import Animated, {
9+
clamp,
910
useSharedValue,
1011
useAnimatedStyle,
1112
withTiming,
1213
} from 'react-native-reanimated';
1314

14-
const clamp = (val, min, max) => Math.min(Math.max(val, min), max);
15-
1615
export default function App() {
1716
const boxWidth = useSharedValue(100);
1817
const distanceDifference = useSharedValue(0);
1918

2019
const centerX = useSharedValue(0);
2120
const centerY = useSharedValue(0);
22-
const maxBoxSize = useSharedValue(0) ;
21+
const maxBoxSize = useSharedValue(0);
2322
const minBoxSize = 20;
2423

2524
const pointerPositionX = useSharedValue(0);
@@ -97,7 +96,7 @@ export default function App() {
9796
}));
9897

9998
return (
100-
<GestureHandlerRootView >
99+
<GestureHandlerRootView>
101100
<View ref={containerRef} style={[styles.container]}>
102101
<GestureDetector gesture={pan}>
103102
<Animated.View

packages/docs-gesture-handler/versioned_docs/version-2.x/examples/FlingGestureBasic.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import {
77
} from 'react-native-gesture-handler';
88
import { StyleSheet, View } from 'react-native';
99
import Animated, {
10+
clamp,
1011
withTiming,
1112
useSharedValue,
1213
useAnimatedStyle,
1314
} from 'react-native-reanimated';
1415

15-
function clamp(val, min, max) {
16-
return Math.min(Math.max(val, min), max);
17-
}
18-
1916
export default function App() {
2017
const translateX = useSharedValue(0);
2118
const startTranslateX = useSharedValue(0);

packages/docs-gesture-handler/versioned_docs/version-2.x/examples/PanGestureBasic.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import Animated, {
33
useSharedValue,
44
useAnimatedStyle,
5+
clamp,
56
} from 'react-native-reanimated';
67
import {
78
Gesture,
@@ -10,10 +11,6 @@ import {
1011
} from 'react-native-gesture-handler';
1112
import { StyleSheet, View } from 'react-native';
1213

13-
function clamp(val, min, max) {
14-
return Math.min(Math.max(val, min), max);
15-
}
16-
1714
export default function App() {
1815
const translationX = useSharedValue(0);
1916
const translationY = useSharedValue(0);
@@ -38,7 +35,7 @@ export default function App() {
3835

3936
containerRef.current.measureInWindow((x, y, width, height) => {
4037
maxTranslateX.value = width / 2 - 50;
41-
maxTranslateY.value = height / 2;
38+
maxTranslateY.value = height / 2 - 50;
4239
});
4340
};
4441

@@ -80,7 +77,7 @@ export default function App() {
8077
});
8178

8279
return (
83-
<GestureHandlerRootView >
80+
<GestureHandlerRootView>
8481
<View ref={containerRef} style={styles.container}>
8582
<GestureDetector gesture={pan}>
8683
<Animated.View style={[animatedStyles, styles.box]}></Animated.View>

packages/docs-gesture-handler/versioned_docs/version-2.x/examples/PinchGestureBasic.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import {
66
} from 'react-native-gesture-handler';
77
import { StyleSheet, View } from 'react-native';
88
import Animated, {
9+
clamp,
910
useSharedValue,
1011
useAnimatedStyle,
1112
withTiming,
1213
} from 'react-native-reanimated';
1314

14-
const clamp = (val, min, max) => Math.min(Math.max(val, min), max);
15-
1615
export default function App() {
1716
const boxWidth = useSharedValue(100);
1817
const distanceDifference = useSharedValue(0);

packages/docs-gesture-handler/versioned_docs/version-2.x/examples/PinchGestureBasicSrc.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import {
66
GestureHandlerRootView,
77
} from 'react-native-gesture-handler';
88
import Animated, {
9+
clamp,
910
useSharedValue,
1011
useAnimatedStyle,
1112
} from 'react-native-reanimated';
1213

1314
const { width, height } = Dimensions.get('screen');
1415

15-
function clamp(val, min, max) {
16-
return Math.min(Math.max(val, min), max);
17-
}
18-
1916
export default function App() {
2017
const scale = useSharedValue(1);
2118
const startScale = useSharedValue(0);

0 commit comments

Comments
 (0)