-
Notifications
You must be signed in to change notification settings - Fork 9
/
App.js
103 lines (94 loc) · 2.42 KB
/
App.js
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 React, { Component } from "react";
import {
StatusBar,
AppRegistry,
StyleSheet,
Text,
View,
PanResponder,
TouchableOpacity,
Dimensions,
} from "react-native";
const { width, height } = Dimensions.get("window");
const getDirectionAndColor = ({ moveX, moveY, dx, dy }) => {
const draggedDown = dy > 30;
const draggedUp = dy < -30;
const draggedLeft = dx < -30;
const draggedRight = dx > 30;
const isRed = moveY < 90 && moveY > 40 && moveX > 0 && moveX < width;
const isBlue = moveY > height - 50 && moveX > 0 && moveX < width;
let dragDirection = "";
if (draggedDown || draggedUp) {
if (draggedDown) dragDirection += "dragged down ";
if (draggedUp) dragDirection += "dragged up ";
}
if (draggedLeft || draggedRight) {
if (draggedLeft) dragDirection += "dragged left ";
if (draggedRight) dragDirection += "dragged right ";
}
if (isRed) return `red ${dragDirection}`;
if (isBlue) return `blue ${dragDirection}`;
if (dragDirection) return dragDirection;
};
export default class App extends Component {
state = {
zone: "Still Touchable",
};
componentWillMount() {
this._panResponder = PanResponder.create({
onMoveShouldSetPanResponder: (evt, gestureState) => !!getDirectionAndColor(gestureState),
onPanResponderMove: (evt, gestureState) => {
const drag = getDirectionAndColor(gestureState);
this.setState({
zone: drag,
});
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
});
}
onPress = () => {
this.setState({
zone: "I got touched with a parent pan responder",
});
};
render() {
return (
<View style={styles.container} {...this._panResponder.panHandlers}>
<StatusBar hidden />
<View style={styles.zone1} />
<View style={styles.center}>
<TouchableOpacity onPress={this.onPress}>
<Text>{this.state.zone}</Text>
</TouchableOpacity>
</View>
<View style={styles.zone2} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
center: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
zone1: {
top: 40,
left: 0,
right: 0,
height: 50,
position: "absolute",
backgroundColor: "red",
},
zone2: {
left: 0,
right: 0,
bottom: 0,
height: 50,
position: "absolute",
backgroundColor: "blue",
},
});