-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathlimit.tsx
67 lines (60 loc) · 1.47 KB
/
limit.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
import * as React from "react";
import { View, StyleSheet } from "react-native";
import Animated from "react-native-reanimated";
import { PanGestureHandler } from "react-native-gesture-handler";
import { limit } from "react-native-redash";
const { Value, event } = Animated;
export default class App extends React.Component {
constructor(props) {
super(props);
this.X = new Value(0);
this.Y = new Value(0);
const dragX = new Value(0);
const dragY = new Value(0);
const panState = new Value(0);
this.handlePan = event([
{
nativeEvent: {
translationX: dragX,
translationY: dragY,
state: panState,
},
},
]);
this.X = limit(dragX, panState, -100, 100);
this.Y = limit(dragY, panState, -100, 100);
}
render() {
return (
<View style={styles.container}>
<PanGestureHandler
onGestureEvent={this.handlePan}
onHandlerStateChange={this.handlePan}
>
<Animated.View
style={[
styles.box,
{
transform: [{ translateX: this.X }, { translateY: this.Y }],
},
]}
/>
</PanGestureHandler>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "#ecf0f1",
padding: 8,
},
box: {
height: 40,
width: 40,
backgroundColor: "red",
},
});