diff --git a/index.android.js b/index.android.js index 7488ce5..7658fb9 100644 --- a/index.android.js +++ b/index.android.js @@ -9,23 +9,77 @@ import { AppRegistry, StyleSheet, Text, - View + 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; +} + class panreject extends Component { + constructor(props) { + super(props) + + this.state = { + zone: "Still Touchable" + } + + this.onPress = this.onPress.bind(this); + } + 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 ( - - - Welcome to React Native! - - - To get started, edit index.android.js - - - Double tap R on your keyboard to reload,{'\n'} - Shake or press menu button for dev menu - + + + + + {this.state.zone} + + + ); } @@ -33,21 +87,29 @@ class panreject extends Component { const styles = StyleSheet.create({ container: { - flex: 1, - justifyContent: 'center', - alignItems: 'center', - backgroundColor: '#F5FCFF', + flex: 1 }, - welcome: { - fontSize: 20, - textAlign: 'center', - margin: 10, + center: { + flex: 1, + alignItems: "center", + justifyContent: "center" }, - instructions: { - textAlign: 'center', - color: '#333333', - marginBottom: 5, + 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" + } }); AppRegistry.registerComponent('panreject', () => panreject);