-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMoveBox.ts
More file actions
39 lines (33 loc) · 978 Bytes
/
useMoveBox.ts
File metadata and controls
39 lines (33 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {
useAnimatedStyle,
useSharedValue,
withSpring,
} from 'react-native-reanimated';
import {Gesture} from 'react-native-gesture-handler';
import {BOX_START_X_POSITION, BOX_START_Y_POSITION} from '~config/box';
function useMoveBox() {
const boxX = useSharedValue(BOX_START_X_POSITION);
const boxY = useSharedValue(BOX_START_Y_POSITION);
const context = useSharedValue({x: 0, y: 0});
const gesture = Gesture.Pan()
.onStart(() => {
context.value = {x: boxX.value, y: boxY.value};
})
.onUpdate(event => {
const eventX = event.translationX + context.value.x;
const eventY = event.translationY + context.value.y;
boxX.value = eventX;
boxY.value = eventY;
});
const style = useAnimatedStyle(
() => ({
transform: [
{translateX: withSpring(boxX.value)},
{translateY: withSpring(boxY.value)},
],
}),
[boxX, boxY],
);
return {gesture, style};
}
export default useMoveBox;