-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.js
99 lines (95 loc) · 2.67 KB
/
Test.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { useEffect, useState } from 'react';
import { View, Button } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated, {
useSharedValue,
withTiming,
useAnimatedStyle,
Easing,
useAnimatedGestureHandler,
withDecay,
runOnJS,
withDelay,
withSpring,
interpolate,
Extrapolate,
} from 'react-native-reanimated';
import { clamp } from 'react-native-redash';
export default function AnimatedStyleUpdateExample(props) {
const [active, setActive] = useState(false);
const activeOpacity = useSharedValue(active ? 1 : 0);
const boundX = 100;
const boundY = 100;
const randomWidth = useSharedValue(10);
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
const config = {
duration: 2000,
easing: Easing.bezier(0.5, 0.01, 0, 1),
};
// const style = useAnimatedStyle(() => {
// return {
// width: withTiming(randomWidth.value, config),
// };
// });
const onActive = (a) => {
console.log(a);
};
const onGestureEvent = useAnimatedGestureHandler({
onStart: (_event, ctx) => {
ctx.offsetX = translateX.value;
ctx.offsetY = translateY.value;
},
onActive: (event, ctx) => {
translateX.value = clamp(ctx.offsetX + event.translationX, 0, boundX);
translateY.value = clamp(ctx.offsetY + event.translationY, 0, boundY);
if (translateX.value > 50) {
runOnJS(onActive)(translateX.value);
}
},
onEnd: ({ velocityX, velocityY }) => {
translateX.value = withDecay({
velocity: velocityX,
clamp: [0, boundX],
});
translateY.value = withDecay({
velocity: velocityY,
clamp: [0, boundY],
});
},
});
const style = useAnimatedStyle(() => {
return {
transform: [
// {
// translateX: withDelay(1000, withTiming(randomWidth.value, config)),
// },
// {
// scale: interpolate(translateX.value, [0, boundX], [1, 2]),
// },
{ translateX: translateX.value },
{ translateY: translateY.value },
],
};
});
useEffect(() => {
activeOpacity.value = withSpring(active ? 1 : 0, 1000);
}, [active]);
return (
<View
style={{
flex: 1,
// alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
}}
>
<PanGestureHandler onGestureEvent={onGestureEvent}>
<Animated.View style={style}>
<View style={[{ width: 50, height: 50, backgroundColor: 'black', margin: 1 }]} />
</Animated.View>
</PanGestureHandler>
<Button title="toggle" onPress={() => setActive((a) => !a)} />
</View>
);
}