UNPKG

nuke-panresponder

Version:

手势

121 lines (103 loc) 2.95 kB
/** @jsx createElement */ import {createElement, Component,findDOMNode} from 'weex-rx'; import { View, Text} from 'nuke-components'; import {mount} from 'nuke-mounter'; import PanResponder from 'nuke-panresponder'; const CIRCLE_SIZE = 80; const CIRCLE_COLOR = 'blue'; const CIRCLE_HIGHLIGHT_COLOR = 'green'; class PanResponderSample extends Component { componentWillMount () { this._panResponder = PanResponder.create({ onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder, onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder, onPanResponderGrant: this._handlePanResponderGrant, onPanResponderMove: this._handlePanResponderMove, onPanResponderRelease: this._handlePanResponderEnd, onPanResponderTerminate: this._handlePanResponderEnd, }); this._previousLeft = 20; this._previousTop = 84; this._circleStyles = { style: { left: this._previousLeft, top: this._previousTop } }; } componentDidMount () { this._updatePosition(); } render () { return ( <View style={styles.container}> <View ref={(circle) => { this.circle = circle; }} style={styles.circle} {...this._panResponder.panHandlers} /> </View> ); } _highlight () { if (this.circle && this.circle.setNativeProps) { this.circle.setNativeProps({ style: { backgroundColor: CIRCLE_HIGHLIGHT_COLOR } }); } } _unHighlight () { if (this.circle && this.circle.setNativeProps) { this.circle.setNativeProps({ style: { backgroundColor: CIRCLE_COLOR } }); } } _updatePosition () { if (this.circle && this.circle.setNativeProps) { this.circle.setNativeProps(this._circleStyles); } } _handleStartShouldSetPanResponder (e, gestureState) { // Should we become active when the user presses down on the circle? return true; } _handleMoveShouldSetPanResponder (e, gestureState) { // Should we become active when the user moves a touch over the circle? return true; } _handlePanResponderGrant = (e, gestureState) => this._highlight(); _handlePanResponderMove = (e, gestureState) => { this._circleStyles.style.left = this._previousLeft + gestureState.dx; this._circleStyles.style.top = this._previousTop + gestureState.dy; this._updatePosition(); }; _handlePanResponderEnd = (e, gestureState) => { this._unHighlight(); this._previousLeft += gestureState.dx; this._previousTop += gestureState.dy; }; } var styles = { circle: { width: CIRCLE_SIZE, height: CIRCLE_SIZE, borderRadius: CIRCLE_SIZE / 2, backgroundColor: CIRCLE_COLOR, position: 'absolute', left: 0, top: 0, }, container: { flex: 1, paddingTop: 64, }, }; mount(<PanResponderSample />, 'body');