简体   繁体   English

TouchableOpacity覆盖PanResponder

[英]TouchableOpacity overwrites PanResponder

I have a PanResponder attached to a TouchableOpacity (that already includes an onPress function). 我有一个PanResponder附加到一个TouchableOpacity(已经包括一个onPress函数)。

This is my PanResponder: 这是我的PanResponder:

this.panResponder = PanResponder.create({
  onMoveShouldSetPanResponder : () => true,
  onPanResponderGrant : (e, gesture) => {
      this.setState({zIndex: 20});
      Animated.spring(this.state.size, {
          toValue: 200,
          friction: 5
      }).start();
  },
  onPanResponderMove           : Animated.event([null,{
      dx : this.state.pan.x,
      dy : this.state.pan.y
  }]),
  onPanResponderRelease        : (e, gesture) => {
      console.log("pan responder release");
  },
  onPanResponderTerminate : (e, gesture) => {
      if(this.state.pan.y._value > 150) {
          Animated.timing(this.state.pan, {
              toValue: { x: 100, y: 200 }
          }).start();
          Animated.timing(this.state.size_1, {
              toValue: 0
          }).start( () => {
              Animated.timing(this.state.size, {
                  toValue: 0,
                  friction: 5
              }).start();
          });
      } else {
          Animated.timing(this.state.size, {
              toValue: 0,
              friction: 5
          }).start();
          Animated.spring(this.state.pan, {
              toValue: { x: 0, y: 0 },
              friction: 5
          }).start(() => {
              this.setState({zIndex: 2})
          });
      }
  },
});

This is my TouchableOpacity: 这是我的TouchableOpacity:

<TouchableOpacity {...this.panResponder.panHandlers} onPress={() => this.refs.modal.open()} style={{position:'absolute', left:10}}>
    <Image
        style={{width: 200, height: 200, borderRadius: 100}}
        source={{uri: 'image url here'}} />
</TouchableOpacity>

When I click on the TouchableOpacity, it works correctly, but I'm not able to drag it as it should according to its PanResponder, but it did work when I had it attached to an Animated.View. 当我单击TouchableOpacity时,它可以正常工作,但是我无法根据其PanResponder对其进行拖动,但是当我将其附加到Animated.View时,它确实起作用了。 Any way I can fix this? 有什么办法可以解决这个问题? I could not find a working solution to this anywhere. 我在任何地方都找不到可行的解决方案。 Thanks! 谢谢!

Try wrapping the TouchableOpacity in Animated.View to which you attach the panResponder : 尝试包裹TouchableOpacityAnimated.View给你附加panResponder

  <Animated.View {...this.panResponder.panHandlers} style={/*style*/}>
    <TouchableOpacity onPress={() => this.refs.modal.open()}>
      <Image
        style={{width: 200, height: 200, borderRadius: 100}}
        source={{uri: 'image url here'}} 
      />
    </TouchableOpacity>
  </Animated.View>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM