-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathExtrapolationExample.tsx
116 lines (105 loc) · 2.82 KB
/
ExtrapolationExample.tsx
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
104
105
106
107
108
109
110
111
112
113
114
115
116
import React from 'react';
import { StyleSheet } from 'react-native';
import Animated, {
useAnimatedGestureHandler,
useAnimatedStyle,
useSharedValue,
interpolate,
withTiming,
Extrapolate,
} from 'react-native-reanimated';
import {
PanGestureHandler,
PanGestureHandlerGestureEvent,
} from 'react-native-gesture-handler';
function ExtrapolationExample(): React.ReactElement {
const translation = {
x: useSharedValue(50),
y: useSharedValue(0),
};
type AnimatedGHContext = {
startX: number;
startY: number;
};
const gestureHandler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
AnimatedGHContext
>({
onStart: (_, ctx) => {
ctx.startX = translation.x.value;
ctx.startY = translation.y.value;
},
onActive: (event, ctx) => {
translation.x.value = ctx.startX + event.translationX;
translation.y.value = ctx.startY + event.translationY;
},
onEnd: (_) => {
translation.x.value = withTiming(50);
translation.y.value = withTiming(0);
},
});
const button1Style = useAnimatedStyle(() => {
const translateX = Math.round(
interpolate(translation.y.value, [0, -75], [0, -75], {
extrapolateLeft: Extrapolate.CLAMP,
extrapolateRight: Extrapolate.EXTEND,
})
);
return {
transform: [{ translateX }, { translateY: translation.y.value }],
};
});
const button2Style = useAnimatedStyle(() => {
const translateY = Math.round(
interpolate(translation.y.value, [0, -75], [0, -150], {
extrapolateLeft: Extrapolate.CLAMP,
extrapolateRight: Extrapolate.EXTEND,
})
);
return {
transform: [{ translateY }],
};
});
const button3Style = useAnimatedStyle(() => {
const translateX = Math.round(
interpolate(translation.y.value, [0, -75], [0, 75], {
extrapolateLeft: Extrapolate.CLAMP,
extrapolateRight: Extrapolate.EXTEND,
})
);
return {
transform: [{ translateX }, { translateY: translation.y.value }],
};
});
const stylez = useAnimatedStyle(() => ({
transform: [{ translateY: translation.y.value }],
}));
return (
<>
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View
style={[
styles.circle,
{ zIndex: 1, backgroundColor: '#001a72' },
stylez,
]}
/>
</PanGestureHandler>
<Animated.View style={[styles.circle, button1Style]} />
<Animated.View style={[styles.circle, button2Style]} />
<Animated.View style={[styles.circle, button3Style]} />
</>
);
}
const styles = StyleSheet.create({
circle: {
width: 50,
height: 50,
position: 'absolute',
top: 400,
alignSelf: 'center',
borderRadius: 25,
backgroundColor: 'red',
},
});
export default ExtrapolationExample;