React-Native触摸组件PanResponder的使用

前端之家收集整理的这篇文章主要介绍了React-Native触摸组件PanResponder的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

已经找到详细文档了:

http://reactnative.cn/docs/0.26/panresponder.html

DEMO:

import React,{ Component } from 'react';
import {
  Alert,StyleSheet,Text,View,Navigator,PanResponder
} from 'react-native';

export default class Login extends Component{
  constructor(props){
    super(props);
    this.state = {
     eventName:'',pos: '',};
    this.myPanResponder={}
  }

  componentWillMount() {
      this.myPanResponder = PanResponder.create({
      //要求成为响应者:
      onStartShouldSetPanResponder: (evt,gestureState) => true,onStartShouldSetPanResponderCapture: (evt,onMoveShouldSetPanResponder: (evt,onMoveShouldSetPanResponderCapture: (evt,onPanResponderTerminationRequest: (evt,//响应对应事件后的处理:
      onPanResponderGrant: (evt,gestureState) => {
        this.state.eventName='触摸开始';
        this.forceUpdate();
      },onPanResponderMove: (evt,gestureState) => {
        var _pos = 'x:' + gestureState.moveX + ',y:' + gestureState.moveY;
        this.setState( {eventName:'移动',pos : _pos} );
      },onPanResponderRelease: (evt,gestureState) => {
        this.setState( {eventName:'抬手'} );
      },onPanResponderTerminate: (evt,gestureState) => {
        this.setState( {eventName:'另一个组件已经成为了新的响应者'} )
      },});
  }

  render(){
    return (
      <View style={styles.container} {...this.myPanResponder.panHandlers}>
        <Text>eventName:{this.state.eventName}|{this.state.pos}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container:{
    backgroundColor:"#fff",flex:1
  }
});

猜你在找的React相关文章