-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathindex.tsx
More file actions
144 lines (131 loc) · 3.85 KB
/
index.tsx
File metadata and controls
144 lines (131 loc) · 3.85 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { Component, PropsWithChildren } from 'react';
import { Animated, StyleSheet, View } from 'react-native';
import {
PanGestureHandler,
RotationGestureHandler,
State,
PanGestureHandlerGestureEvent,
PanGestureHandlerStateChangeEvent,
RotationGestureHandlerGestureEvent,
RotationGestureHandlerStateChangeEvent,
} from 'react-native-gesture-handler';
import { USE_NATIVE_DRIVER } from '../../config';
class Snappable extends Component<PropsWithChildren<Record<string, unknown>>> {
private onGestureEvent?: (event: PanGestureHandlerGestureEvent) => void;
private transX: Animated.AnimatedInterpolation<number>;
private dragX: Animated.Value;
constructor(props: Record<string, unknown>) {
super(props);
this.dragX = new Animated.Value(0);
this.transX = this.dragX.interpolate({
inputRange: [-100, -50, 0, 50, 100],
outputRange: [-30, -10, 0, 10, 30],
});
this.onGestureEvent = Animated.event(
[{ nativeEvent: { translationX: this.dragX } }],
{ useNativeDriver: USE_NATIVE_DRIVER }
);
}
private onHandlerStateChange = (event: PanGestureHandlerStateChangeEvent) => {
if (event.nativeEvent.oldState === State.ACTIVE) {
Animated.spring(this.dragX, {
velocity: event.nativeEvent.velocityX,
tension: 10,
friction: 2,
toValue: 0,
useNativeDriver: USE_NATIVE_DRIVER,
}).start();
}
};
render() {
const { children } = this.props;
return (
<PanGestureHandler
{...this.props}
maxPointers={1}
onGestureEvent={this.onGestureEvent}
onHandlerStateChange={this.onHandlerStateChange}>
<Animated.View style={{ transform: [{ translateX: this.transX }] }}>
{children}
</Animated.View>
</PanGestureHandler>
);
}
}
class Twistable extends Component<PropsWithChildren<unknown>> {
private gesture: Animated.Value;
private onGestureEvent?: (event: RotationGestureHandlerGestureEvent) => void;
private rot: Animated.AnimatedInterpolation<number>;
constructor(props: Record<string, unknown>) {
super(props);
this.gesture = new Animated.Value(0);
this.rot = this.gesture
.interpolate({
inputRange: [-1.2, -1, -0.5, 0, 0.5, 1, 1.2],
outputRange: [-0.52, -0.5, -0.3, 0, 0.3, 0.5, 0.52],
})
.interpolate({
inputRange: [-100, 100],
outputRange: ['-5700deg', '5700deg'],
});
this.onGestureEvent = Animated.event(
[{ nativeEvent: { rotation: this.gesture } }],
{ useNativeDriver: USE_NATIVE_DRIVER }
);
}
private onHandlerStateChange = (
event: RotationGestureHandlerStateChangeEvent
) => {
if (event.nativeEvent.oldState === State.ACTIVE) {
Animated.spring(this.gesture, {
velocity: event.nativeEvent.velocity,
tension: 10,
friction: 0.2,
toValue: 0,
useNativeDriver: USE_NATIVE_DRIVER,
}).start();
}
};
render() {
const { children } = this.props;
return (
<RotationGestureHandler
{...this.props}
onGestureEvent={this.onGestureEvent}
onHandlerStateChange={this.onHandlerStateChange}>
<Animated.View style={{ transform: [{ rotate: this.rot }] }}>
{children}
</Animated.View>
</RotationGestureHandler>
);
}
}
export default class Example extends Component {
render() {
return (
<View style={styles.container}>
<Snappable>
<Twistable>
<View style={styles.box} />
</Twistable>
</Snappable>
</View>
);
}
}
const BOX_SIZE = 200;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
box: {
width: BOX_SIZE,
height: BOX_SIZE,
borderColor: '#F5FCFF',
alignSelf: 'center',
backgroundColor: 'plum',
margin: BOX_SIZE / 2,
},
});