Panresponder可以继承Touchable touch事件吗?

时间:2018-11-14 12:12:01

标签: react-native react-native-android expo react-native-ios

在本机反应中,我有一个按钮,在pressIn上可以打开模式。在模式中,我有一个带有Panresponder手柄的“视图”,它允许在模式内上下拖动一个框。

这工作正常,但是我必须在模态打开时再进行一次触摸,以便Panresponder View拾取触摸并开始拖动。有没有一种方法可以将触摸事件传递给模态,或者在手指仍然在屏幕上的情况下让模式内部的Panresponder View拾取触摸?

示例代码:

class Parent extends Component {
  state = {
    modalVisible: false
  };
  render() {
    <View>
      {/* When this is pressed...  */}
      <TouchableOpacity onPressIn={() => this.setState({ modalVisible: true })}>
        <Text>Open & start dragging</Text>
      </TouchableOpacity>
      {/* Is there a way to capture the touch event right away?  */}
      <DragModal visible={this.state.modalVisible} />
    </View>;
  }
}

class DragModal extends Component {
  translateY = new Animated.Value(0);
  panresponder = PanResponder.create({
    // Ask to be the responder:
    onStartShouldSetPanResponder: (evt, gestureState) => true,
    onPanResponderMove: Animated.event([
      null,
      {
        moveY: this.y
      }
    ])
    // ...etc
  });
  render() {
    return (
      <Modal visible={this.props.visible}>
        <View {...this.panresponder.panHandlers} style={styles.container}>
          <View
            style={{
              ...styles.dragHandle,
              transform: [{ translateY: this.translateY }]
            }}
          >
            <Text>Draghandle</Text>
          </View>
        </View>
      </Modal>
    );
  }
}

0 个答案:

没有答案