-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.tsx
More file actions
103 lines (90 loc) · 2.29 KB
/
index.tsx
File metadata and controls
103 lines (90 loc) · 2.29 KB
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
100
101
102
103
import { StyleSheet, View } from 'react-native';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import Animated, {
interpolateColor,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
const Durations = {
LongPress: 750,
Reset: 350,
Scale: 120,
};
const Colors = {
Initial: '#0a2688',
Loading: '#6fcef5',
Success: '#32a852',
Fail: '#b02525',
};
export default function LongPressExample() {
const isPressed = useSharedValue(false);
const colorProgress = useSharedValue(0);
const color1 = useSharedValue(Colors.Initial);
const color2 = useSharedValue(Colors.Loading);
const animatedStyles = useAnimatedStyle(() => {
const backgroundColor = interpolateColor(
colorProgress.value,
[0, 1],
[color1.value, color2.value]
);
return {
transform: [
{
scale: withTiming(isPressed.value ? 1.2 : 1, {
duration: Durations.Scale,
}),
},
],
backgroundColor,
};
});
const g = Gesture.LongPress()
.onBegin(() => {
console.log('onBegin');
isPressed.value = true;
colorProgress.value = withTiming(1, {
duration: Durations.LongPress,
});
})
.onStart(() => console.log('onStart'))
.onEnd(() => console.log('onEnd'))
.onFinalize((_, success) => {
console.log('onFinalize', success);
isPressed.value = false;
color1.value = Colors.Initial;
color2.value = success ? Colors.Success : Colors.Fail;
colorProgress.value = withTiming(
0,
{
duration: Durations.Reset,
},
() => {
color2.value = Colors.Loading;
}
);
})
.onTouchesDown(() => console.log('onTouchesDown'))
.onTouchesMove(() => console.log('onTouchesMove'))
.onTouchesUp(() => console.log('onTouchesUp'))
.minDuration(Durations.LongPress);
return (
<View style={styles.container}>
<GestureDetector gesture={g}>
<Animated.View style={[styles.pressBox, animatedStyles]} />
</GestureDetector>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-around',
alignItems: 'center',
},
pressBox: {
width: 100,
height: 100,
borderRadius: 20,
},
});