Skip to content

Commit c3d0441

Browse files
committed
Merge 2 steps
1 parent 1b930d5 commit c3d0441

7 files changed

Lines changed: 86 additions & 104 deletions

File tree

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
```jsx
2+
import { StyleSheet } from 'react-native';
23
import { GestureHandlerRootView } from 'react-native-gesture-handler';
34
import Animated from 'react-native-reanimated';
45

56
export default function Ball() {
67
return (
78
<GestureHandlerRootView>
8-
<Animated.View />
9+
<Animated.View style={styles.ball} />
910
</GestureHandlerRootView>
1011
);
1112
}
13+
14+
const styles = StyleSheet.create({
15+
ball: {
16+
width: 100,
17+
height: 100,
18+
borderRadius: 100,
19+
backgroundColor: 'blue',
20+
alignSelf: 'center',
21+
},
22+
});
1223
```
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
```jsx
2-
import { StyleSheet } from 'react-native';
3-
import { GestureHandlerRootView } from 'react-native-gesture-handler';
4-
import Animated from 'react-native-reanimated';
2+
import {
3+
useAnimatedStyle,
4+
useSharedValue,
5+
withSpring,
6+
} from 'react-native-reanimated';
57

68
export default function Ball() {
7-
return (
8-
<GestureHandlerRootView>
9-
<Animated.View style={styles.ball} />
10-
</GestureHandlerRootView>
11-
);
12-
}
9+
const isPressed = useSharedValue(false);
10+
const offset = useSharedValue({ x: 0, y: 0 });
11+
12+
const animatedStyles = useAnimatedStyle(() => {
13+
return {
14+
transform: [
15+
{ translateX: offset.value.x },
16+
{ translateY: offset.value.y },
17+
{ scale: withSpring(isPressed.value ? 1.2 : 1) },
18+
],
19+
backgroundColor: isPressed.value ? 'yellow' : 'blue',
20+
};
21+
});
1322

14-
const styles = StyleSheet.create({
15-
ball: {
16-
width: 100,
17-
height: 100,
18-
borderRadius: 100,
19-
backgroundColor: 'blue',
20-
alignSelf: 'center',
21-
},
22-
});
23+
// ...
24+
}
2325
```
Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
1-
```jsx
2-
import {
3-
useAnimatedStyle,
4-
useSharedValue,
5-
withSpring,
6-
} from 'react-native-reanimated';
7-
8-
export default function Ball() {
9-
const isPressed = useSharedValue(false);
10-
const offset = useSharedValue({ x: 0, y: 0 });
11-
12-
const animatedStyles = useAnimatedStyle(() => {
13-
return {
14-
transform: [
15-
{ translateX: offset.value.x },
16-
{ translateY: offset.value.y },
17-
{ scale: withSpring(isPressed.value ? 1.2 : 1) },
18-
],
19-
backgroundColor: isPressed.value ? 'yellow' : 'blue',
20-
};
21-
});
22-
23-
// ...
24-
}
1+
```jsx {4}
2+
// ...
3+
return (
4+
<GestureHandlerRootView>
5+
<Animated.View style={[styles.ball, animatedStyles]} />
6+
</GestureHandlerRootView>
7+
);
8+
// ...
259
```
Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
```jsx {4}
2-
// ...
3-
return (
4-
<GestureHandlerRootView>
5-
<Animated.View style={[styles.ball, animatedStyles]} />
6-
</GestureHandlerRootView>
7-
);
8-
// ...
1+
```jsx
2+
import { usePanGesture } from 'react-native-gesture-handler';
3+
4+
function Ball() {
5+
// ...
6+
const start = useSharedValue({ x: 0, y: 0 });
7+
8+
const gesture = usePanGesture({
9+
onBegin: () => {
10+
isPressed.value = true;
11+
},
12+
onUpdate: (e) => {
13+
offset.value = {
14+
x: e.translationX + start.value.x,
15+
y: e.translationY + start.value.y,
16+
};
17+
},
18+
onDeactivate: () => {
19+
start.value = {
20+
x: offset.value.x,
21+
y: offset.value.y,
22+
};
23+
},
24+
onFinalize: () => {
25+
isPressed.value = false;
26+
},
27+
});
28+
// ...
29+
}
930
```
Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
```jsx
2-
import { usePanGesture } from 'react-native-gesture-handler';
3-
4-
function Ball() {
5-
// ...
6-
const start = useSharedValue({ x: 0, y: 0 });
7-
8-
const gesture = usePanGesture({
9-
onBegin: () => {
10-
isPressed.value = true;
11-
},
12-
onUpdate: (e) => {
13-
offset.value = {
14-
x: e.translationX + start.value.x,
15-
y: e.translationY + start.value.y,
16-
};
17-
},
18-
onDeactivate: () => {
19-
start.value = {
20-
x: offset.value.x,
21-
y: offset.value.y,
22-
};
23-
},
24-
onFinalize: () => {
25-
isPressed.value = false;
26-
},
27-
});
28-
// ...
29-
}
1+
```jsx {4}
2+
// ...
3+
return (
4+
<GestureHandlerRootView>
5+
<GestureDetector gesture={gesture}>
6+
<Animated.View style={[styles.ball, animatedStyles]} />
7+
</GestureDetector>
8+
</GestureHandlerRootView>
9+
);
10+
// ...
3011
```

packages/docs-gesture-handler/docs/guides/quickstart/_steps/step6.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/docs-gesture-handler/docs/guides/quickstart/index.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Step2 from './\_steps/step2.md';
1111
import Step3 from './\_steps/step3.md';
1212
import Step4 from './\_steps/step4.md';
1313
import Step5 from './\_steps/step5.md';
14-
import Step6 from './\_steps/step6.md';
1514

1615
RNGH3 offers a straightforward way to add gestures to your app. Simply wrap your target view with the [GestureDetector](/docs/fundamentals/gesture-detectors#gesture-detector) component, define your gesture, and pass it in. That’s it!
1716

@@ -23,34 +22,29 @@ To see the new API in action, let's build a simple app where you can drag a ball
2322
</Step>
2423

2524
<Step title="Step 2">
26-
<div>Next, let's add the necessary styles:</div>
27-
<Step2 />
28-
</Step>
29-
30-
<Step title="Step 3">
3125
<div>
3226
Next, define the <a href="https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#shared-value">shared values</a> to track the ball's position and create the animated styles required to position the ball on the screen:
3327
</div>
34-
<Step3 />
28+
<Step2 />
3529
</Step>
3630

37-
<Step title="Step 4">
31+
<Step title="Step 3">
3832
<div>Apply the animated styles to the ball component:</div>
39-
<Step4 />
33+
<Step3 />
4034
</Step>
4135

42-
<Step title="Step 5">
36+
<Step title="Step 4">
4337
<div>
4438
Now, define the <code>Pan</code> gesture logic:
4539
</div>
46-
<Step5 />
40+
<Step4 />
4741
</Step>
4842

49-
<Step title="Step 6">
43+
<Step title="Step 5">
5044
<div>
5145
Finally, assign the <code>Pan</code> gesture to the <code>GestureDetector</code>:
5246
</div>
53-
<Step6 />
47+
<Step5 />
5448
</Step>
5549

5650
Note the `start` shared value. We need it to store the position of the ball at the moment we grab it to be able to correctly position it later, because we only have access to translation relative to the starting point of the gesture.

0 commit comments

Comments
 (0)