|
| 1 | +/** |
| 2 | + * @flow |
| 3 | + */ |
| 4 | + |
| 5 | +import React, { PureComponent } from 'react'; |
| 6 | +import { PanResponder, StyleSheet, View } from 'react-native'; |
| 7 | + |
| 8 | +const CIRCLE_SIZE = 80; |
| 9 | + |
| 10 | +export default class DraggableCircle extends PureComponent { |
| 11 | + _panResponder = {}; |
| 12 | + _previousLeft = 0; |
| 13 | + _previousTop = 0; |
| 14 | + _circleStyles = {}; |
| 15 | + circle = (null: ?{ setNativeProps(props: Object): void }); |
| 16 | + |
| 17 | + constructor() { |
| 18 | + super(); |
| 19 | + this._panResponder = PanResponder.create({ |
| 20 | + onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder, |
| 21 | + onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder, |
| 22 | + onPanResponderGrant: this._handlePanResponderGrant, |
| 23 | + onPanResponderMove: this._handlePanResponderMove, |
| 24 | + onPanResponderRelease: this._handlePanResponderEnd, |
| 25 | + onPanResponderTerminate: this._handlePanResponderEnd |
| 26 | + }); |
| 27 | + this._previousLeft = 20; |
| 28 | + this._previousTop = 84; |
| 29 | + this._circleStyles = { |
| 30 | + style: { |
| 31 | + left: this._previousLeft, |
| 32 | + top: this._previousTop, |
| 33 | + backgroundColor: 'green' |
| 34 | + } |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + componentDidMount() { |
| 39 | + this._updateNativeStyles(); |
| 40 | + } |
| 41 | + |
| 42 | + render() { |
| 43 | + return ( |
| 44 | + <View style={styles.container}> |
| 45 | + <View ref={this._setCircleRef} style={styles.circle} {...this._panResponder.panHandlers} /> |
| 46 | + </View> |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + _setCircleRef = circle => { |
| 51 | + this.circle = circle; |
| 52 | + }; |
| 53 | + |
| 54 | + _highlight() { |
| 55 | + this._circleStyles.style.backgroundColor = 'blue'; |
| 56 | + this._updateNativeStyles(); |
| 57 | + } |
| 58 | + |
| 59 | + _unHighlight() { |
| 60 | + this._circleStyles.style.backgroundColor = 'green'; |
| 61 | + this._updateNativeStyles(); |
| 62 | + } |
| 63 | + |
| 64 | + _updateNativeStyles() { |
| 65 | + this.circle && this.circle.setNativeProps(this._circleStyles); |
| 66 | + } |
| 67 | + |
| 68 | + _handleStartShouldSetPanResponder = (e: Object, gestureState: Object): boolean => { |
| 69 | + // Should we become active when the user presses down on the circle? |
| 70 | + return true; |
| 71 | + }; |
| 72 | + |
| 73 | + _handleMoveShouldSetPanResponder = (e: Object, gestureState: Object): boolean => { |
| 74 | + // Should we become active when the user moves a touch over the circle? |
| 75 | + return true; |
| 76 | + }; |
| 77 | + |
| 78 | + _handlePanResponderGrant = (e: Object, gestureState: Object) => { |
| 79 | + this._highlight(); |
| 80 | + }; |
| 81 | + |
| 82 | + _handlePanResponderMove = (e: Object, gestureState: Object) => { |
| 83 | + this._circleStyles.style.left = this._previousLeft + gestureState.dx; |
| 84 | + this._circleStyles.style.top = this._previousTop + gestureState.dy; |
| 85 | + this._updateNativeStyles(); |
| 86 | + }; |
| 87 | + |
| 88 | + _handlePanResponderEnd = (e: Object, gestureState: Object) => { |
| 89 | + this._unHighlight(); |
| 90 | + this._previousLeft += gestureState.dx; |
| 91 | + this._previousTop += gestureState.dy; |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +const styles = StyleSheet.create({ |
| 96 | + circle: { |
| 97 | + width: CIRCLE_SIZE, |
| 98 | + height: CIRCLE_SIZE, |
| 99 | + borderRadius: CIRCLE_SIZE / 2, |
| 100 | + position: 'absolute', |
| 101 | + left: 0, |
| 102 | + top: 0, |
| 103 | + touchAction: 'none' |
| 104 | + }, |
| 105 | + container: { |
| 106 | + flex: 1, |
| 107 | + minHeight: 400, |
| 108 | + paddingTop: 64 |
| 109 | + } |
| 110 | +}); |
0 commit comments