3

我正在使用 react-native-svg,因为文档说可以在 svg 元素上触发 onPress 事件,但它不起作用。

<Svg width={370} height={180} {...props} viewBox="0 0 385 185" >
   <Path
      d="M5.607 13.536l9.267 9.267a2.5 2.5 0 0 1-3.535 3.536L.732 15.732a2.497 2.497 0 0 1-.695-2.196 2.497 2.497 0 0 1 .695-2.197L11.34.732a2.5 2.5 0 0 1 3.535 3.536l-9.267 9.268z"
      fill="white"
      onPress={() => alert('Press on Circle')}
    />
</Svg>

然后我尝试了文档中编写的示例:

<Svg
            height="100"
            width="100"
        >
    <Circle
    cx="50%"
    cy="50%"
    r="38%"
    fill="red"
    onPress={() => alert('Press on Circle')}
/>
  </Svg>

但是作为第一个它不起作用

4

1 回答 1

-1

问题是由 panResponder 引起的,因为它的优先级高于按钮,因此解决方案是不要从 onMoveShouldSetPanResponderCapture 始终返回 true。前任:

componentWillMount() {
        this.panResponder = PanResponder.create({
            onMoveShouldSetPanResponderCapture: this.onShouldDrag,
            onPanResponderMove: Animated.event(
            [null, { dx: this.state.pan.x }]
            ),
            onPanResponderRelease: this.onReleaseItem,
            onPanResponderTerminate: this.onReleaseItem,
        });
    }

    onShouldDrag = (e, gesture) => {
        const { dx } = gesture;
        return Math.abs(dx) > 2;
    }
于 2018-12-24T11:08:43.267 回答