在线文档教程
React native

PanResponder

PanResponder

PanResponder将几次触摸调和成一个手势。它使单点触摸手势具有弹性,可用于识别简单的多点触摸手势。

默认情况下,PanResponder持有InteractionManager阻止长时间运行的JS事件中断活动手势的句柄。

它提供了姿态响应系统提供的响应者处理程序的可预测包装。对于每个处理程序,它会gestureState在本机事件对象旁边提供一个新对象:

onPanResponderMove: (event, gestureState) => {}

本地事件是以下形式的合成触摸事件:

  • nativeEvent

一个gestureState对象具有以下内容:

  • stateID - 只要屏幕上至少有一次触摸,gestureState的ID就会保持

基本用法

componentWillMount: function() { this._panResponder = PanResponder.create{ // Ask to be the responder: onStartShouldSetPanResponder: (evt, gestureState) => true, onStartShouldSetPanResponderCapture: (evt, gestureState) => true, onMoveShouldSetPanResponder: (evt, gestureState) => true, onMoveShouldSetPanResponderCapture: (evt, gestureState) => true, onPanResponderGrant: (evt, gestureState) => { // The gesture has started. Show visual feedback so the user knows // what is happening! // gestureState.d{x,y} will be set to zero now }, onPanResponderMove: (evt, gestureState) => { // The most recent move distance is gestureState.move{X,Y} // The accumulated gesture distance since becoming responder is // gestureState.d{x,y} }, onPanResponderTerminationRequest: (evt, gestureState) => true, onPanResponderRelease: (evt, gestureState) => { // The user has released all touches while this view is the // responder. This typically means a gesture has succeeded }, onPanResponderTerminate: (evt, gestureState) => { // Another component has become the responder, so this gesture // should be cancelled }, onShouldBlockNativeResponder: (evt, gestureState) => { // Returns whether this component should block native components from becoming the JS // responder. Returns true by default. Is currently only supported on android. return true; }, } }, render: function() { return ( <View {...this._panResponder.panHandlers} /> },

工作示例

要查看它的实际运行情况,请尝试使用RNTester中PanResponder示例

方法

静态创建(配置)

@param {object} config所有响应者回调的增强版本,它们不仅提供了典型的ResponderSyntheticEvent,还提供了PanResponder手势状态。只需更换这个词ResponderPanResponder在每一个典型的onResponder*回调。例如,该config对象看起来像:

  • onMoveShouldSetPanResponder: (e, gestureState) => {...}