-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.tsx
More file actions
172 lines (163 loc) · 5.45 KB
/
index.tsx
File metadata and controls
172 lines (163 loc) · 5.45 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React from 'react';
import { Animated, Platform, StyleSheet } from 'react-native';
import {
PanGestureHandler,
PinchGestureHandler,
RotationGestureHandler,
State,
PanGestureHandlerGestureEvent,
PanGestureHandlerStateChangeEvent,
RotationGestureHandlerGestureEvent,
PinchGestureHandlerGestureEvent,
PinchGestureHandlerStateChangeEvent,
RotationGestureHandlerStateChangeEvent,
} from 'react-native-gesture-handler';
import { USE_NATIVE_DRIVER } from '../../config';
export class PinchableBox extends React.Component {
private panRef = React.createRef<PanGestureHandler>();
private rotationRef = React.createRef<RotationGestureHandler>();
private pinchRef = React.createRef<PinchGestureHandler>();
private baseScale: Animated.Value;
private pinchScale: Animated.Value;
private scale: Animated.AnimatedMultiplication<number>;
private lastScale: number;
private onPinchGestureEvent: (event: PinchGestureHandlerGestureEvent) => void;
private rotate: Animated.Value;
private rotateStr: Animated.AnimatedInterpolation<number>;
private lastRotate: number;
private onRotateGestureEvent: (
event: RotationGestureHandlerGestureEvent
) => void;
private tilt: Animated.Value;
private tiltStr: Animated.AnimatedMultiplication<number>;
private lastTilt: number;
private onTiltGestureEvent: (event: PanGestureHandlerGestureEvent) => void;
constructor(props: Record<string, unknown>) {
super(props);
/* Pinching */
this.baseScale = new Animated.Value(1);
this.pinchScale = new Animated.Value(1);
this.scale = Animated.multiply(this.baseScale, this.pinchScale);
this.lastScale = 1;
this.onPinchGestureEvent = Animated.event(
[{ nativeEvent: { scale: this.pinchScale } }],
{ useNativeDriver: USE_NATIVE_DRIVER }
);
/* Rotation */
this.rotate = new Animated.Value(0);
this.rotateStr = this.rotate.interpolate({
inputRange: [-100, 100],
outputRange: ['-5700deg', '5700deg'],
});
this.lastRotate = 0;
this.onRotateGestureEvent = Animated.event(
[{ nativeEvent: { rotation: this.rotate } }],
{ useNativeDriver: USE_NATIVE_DRIVER }
);
/* Tilt */
this.tilt = new Animated.Value(0);
this.tiltStr = this.tilt.interpolate({
inputRange: [-501, -500, 0, 1],
outputRange: ['57deg', '57deg', '0deg', '0deg'],
});
this.lastTilt = 0;
this.onTiltGestureEvent = Animated.event(
[{ nativeEvent: { translationY: this.tilt } }],
{ useNativeDriver: USE_NATIVE_DRIVER }
);
}
private onRotateHandlerStateChange = (
event: RotationGestureHandlerStateChangeEvent
) => {
if (event.nativeEvent.oldState === State.ACTIVE) {
this.lastRotate += event.nativeEvent.rotation;
this.rotate.setOffset(this.lastRotate);
this.rotate.setValue(0);
}
};
private onPinchHandlerStateChange = (
event: PinchGestureHandlerStateChangeEvent
) => {
if (event.nativeEvent.oldState === State.ACTIVE) {
this.lastScale *= event.nativeEvent.scale;
this.baseScale.setValue(this.lastScale);
this.pinchScale.setValue(1);
}
};
private onTiltGestureStateChange = (
event: PanGestureHandlerStateChangeEvent
) => {
if (event.nativeEvent.oldState === State.ACTIVE) {
this.lastTilt += event.nativeEvent.translationY;
this.tilt.setOffset(this.lastTilt);
this.tilt.setValue(0);
}
};
render() {
return (
<PanGestureHandler
ref={this.panRef}
onGestureEvent={this.onTiltGestureEvent}
onHandlerStateChange={this.onTiltGestureStateChange}
minDist={10}
minPointers={2}
maxPointers={2}
avgTouches>
<Animated.View style={styles.wrapper}>
<RotationGestureHandler
ref={this.rotationRef}
simultaneousHandlers={this.pinchRef}
onGestureEvent={this.onRotateGestureEvent}
onHandlerStateChange={this.onRotateHandlerStateChange}>
<Animated.View style={styles.wrapper}>
<PinchGestureHandler
ref={this.pinchRef}
simultaneousHandlers={this.rotationRef}
onGestureEvent={this.onPinchGestureEvent}
onHandlerStateChange={this.onPinchHandlerStateChange}>
<Animated.View style={styles.container} collapsable={false}>
<Animated.Image
style={[
styles.pinchableImage,
{
transform: [
{ perspective: 200 },
{ scale: this.scale },
{ rotate: this.rotateStr },
{ rotateX: this.tiltStr },
],
},
]}
source={
Platform.OS === 'web'
? require('./swm.svg')
: require('./swmansion.png')
}
/>
</Animated.View>
</PinchGestureHandler>
</Animated.View>
</RotationGestureHandler>
</Animated.View>
</PanGestureHandler>
);
}
}
export default PinchableBox;
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
backgroundColor: 'black',
overflow: 'hidden',
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
pinchableImage: {
width: 250,
height: 250,
},
wrapper: {
flex: 1,
},
});