如何在子组件上禁用PanResponder - React Native

时间:2017-08-22 06:23:29

标签: reactjs react-native react-native-android react-native-ios react-native-0.46

朋友我将采取一个父母视图和视图内部我采取一个TouchableOpacity。我将在主视图上添加panResponder,并且当触摸主视图时它的工作正常。但是TouchableOpacity无效,我想在TouchableOpacity上删除panResponder。所以TouchableOpacity正常运作。

代码:

    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

        // this.toggleModal()
      },
      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}
        console.log('onPanResponderMove');
      },
      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
        console.log('onPanResponderRelease');

        this.toggleModal()
      },
      onPanResponderTerminate: (evt, gestureState) => {
        // Another component has become the responder, so this gesture
        // should be cancelled

        console.log('onPanResponderTerminate');

      },
      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.

        console.log('onShouldBlockNativeResponder');

        return true;
      },
    });
 }

Pan在父母视图中添加:

render() {
    return (
      <View {...this._panResponder.panHandlers}>
        <TouchableOpacity onPress={this._onPressButton}>
         <Image
          style={styles.button}
          source={require('./myButton.png')}
         />
       </TouchableOpacity>
      </View>
    );
  },

在mainView上添加Pan时,Button OnPress事件不起作用。

1 个答案:

答案 0 :(得分:3)

更改此行,然后内部Touchables将获得该事件。

当返回true时,会阻止孩子成为回应者。

  onStartShouldSetPanResponderCapture: (evt, gestureState) => false,

来自document

  

因此,如果父View希望阻止孩子成为响应者   在触摸开始时,它应该有一个onStartShouldSetResponderCapture   处理程序返回true。