Skip to content

Commit bd46baa

Browse files
committed
Create Square component
1 parent 89809a5 commit bd46baa

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

reactnative/App.js

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,6 @@
11
import { StyleSheet } from "react-native";
2-
import {
3-
Gesture,
4-
GestureDetector,
5-
GestureHandlerRootView,
6-
} from "react-native-gesture-handler";
7-
import Animated, {
8-
useAnimatedStyle,
9-
useSharedValue,
10-
withSpring,
11-
} from "react-native-reanimated";
12-
13-
function Square() {
14-
const isPressed = useSharedValue(false);
15-
const startPos = useSharedValue({ x: 0, y: 0 });
16-
const pos = useSharedValue({ x: 0, y: 0 });
17-
const animStyle = useAnimatedStyle(() => {
18-
return {
19-
backgroundColor: isPressed.value ? "red" : "blue",
20-
transform: [
21-
{ translateX: pos.value.x },
22-
{ translateY: pos.value.y },
23-
{ scale: withSpring(isPressed.value ? 1.2 : 1) },
24-
],
25-
};
26-
});
27-
28-
const gesture = Gesture.Pan()
29-
.onBegin(() => {
30-
isPressed.value = true;
31-
})
32-
.onChange((e) => {
33-
pos.value = {
34-
x: startPos.value.x + e.translationX,
35-
y: startPos.value.y + e.translationY,
36-
};
37-
})
38-
.onFinalize(() => {
39-
isPressed.value = false;
40-
startPos.value = {
41-
x: pos.value.x,
42-
y: pos.value.y,
43-
};
44-
});
45-
46-
return (
47-
<GestureDetector gesture={gesture}>
48-
<Animated.View style={[styles.square, animStyle]} />
49-
</GestureDetector>
50-
);
51-
}
2+
import { GestureHandlerRootView } from "react-native-gesture-handler";
3+
import Square from "./components/Square";
524

535
export default function App() {
546
return (
@@ -67,9 +19,4 @@ const styles = StyleSheet.create({
6719
alignItems: "center",
6820
justifyContent: "space-evenly",
6921
},
70-
square: {
71-
width: 100,
72-
height: 100,
73-
backgroundColor: "blue",
74-
},
7522
});

reactnative/components/Square.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { StyleSheet } from "react-native";
2+
import { Gesture, GestureDetector } from "react-native-gesture-handler";
3+
4+
import Animated, {
5+
useAnimatedStyle,
6+
useSharedValue,
7+
withSpring,
8+
} from "react-native-reanimated";
9+
10+
export default function Square() {
11+
const isPressed = useSharedValue(false);
12+
const startPos = useSharedValue({ x: 0, y: 0 });
13+
const pos = useSharedValue({ x: 0, y: 0 });
14+
const animStyle = useAnimatedStyle(() => {
15+
return {
16+
backgroundColor: isPressed.value ? "red" : "blue",
17+
transform: [
18+
{ translateX: pos.value.x },
19+
{ translateY: pos.value.y },
20+
{ scale: withSpring(isPressed.value ? 1.2 : 1) },
21+
],
22+
};
23+
});
24+
25+
const gesture = Gesture.Pan()
26+
.onBegin(() => {
27+
isPressed.value = true;
28+
})
29+
.onChange((e) => {
30+
pos.value = {
31+
x: startPos.value.x + e.translationX,
32+
y: startPos.value.y + e.translationY,
33+
};
34+
})
35+
.onFinalize(() => {
36+
isPressed.value = false;
37+
startPos.value = {
38+
x: pos.value.x,
39+
y: pos.value.y,
40+
};
41+
});
42+
43+
return (
44+
<GestureDetector gesture={gesture}>
45+
<Animated.View style={[styles.container, animStyle]} />
46+
</GestureDetector>
47+
);
48+
}
49+
50+
const styles = StyleSheet.create({
51+
container: {
52+
width: 100,
53+
height: 100,
54+
backgroundColor: "blue",
55+
},
56+
});

0 commit comments

Comments
 (0)