标签:release highlight reac handler top rmi start cti this
通过添加手势事件
// dx 和 dy:从触摸操作开始到现在的累积横向/纵向路程 // // moveX 和 moveY:最近一次移动时的屏幕横/纵坐标 // // numberActiveTouches:当前在屏幕上的有效触摸点的数量 // // stated:和之前一样,用来识别手指的ID // // vx 和 vy:当前横向/纵向移动的速度 // // x0 和 y0:当触摸操作开始时组件相对于屏幕的横/纵坐标 import React, {PureComponent, Component} from ‘react‘; import { AppRegistry, StyleSheet, Text, View, PanResponder, } from ‘react-native‘; export default class TouchStartAndRelease extends PureComponent { constructor(props) { super(props); this.state = { backgroundColor: ‘red‘, marginTop: 100, marginLeft: 100, }; this.lastX = this.state.marginLeft; this.lastY = this.state.marginTop; } componentWillMount(){ this._panResponder = PanResponder.create({ onStartShouldSetPanResponder: (evt, gestureState) => { return true; }, onMoveShouldSetPanResponder: (evt, gestureState) => { return true; }, onPanResponderGrant: (evt, gestureState) => { this._highlight(); }, onPanResponderMove: (evt, gestureState) => { console.log(`gestureState.dx : ${gestureState.dx} gestureState.dy : ${gestureState.dy}`); this.setState({ marginLeft: this.lastX + gestureState.dx, marginTop: this.lastY + gestureState.dy, }); }, onPanResponderRelease: (evt, gestureState) => { this._unhighlight(); this.lastX = this.state.marginLeft; this.lastY = this.state.marginTop; }, onPanResponderTerminate: (evt, gestureState) => { }, }); } _unhighlight(){ this.setState({ backgroundColor: ‘red‘, }); } _highlight(){ this.setState({ backgroundColor: ‘blue‘, }); } render() { return ( <View style={styles.container}> <View style={[styles.redView, { backgroundColor: this.state.backgroundColor, marginTop: this.state.marginTop, marginLeft: this.state.marginLeft, } ]} {...this._panResponder.panHandlers} /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, }, redView: { width: 100, height: 100, }, });
标签:release highlight reac handler top rmi start cti this
原文地址:https://www.cnblogs.com/jingguorui/p/13525306.html