reactjs - react-native-svg onPress 事件不工作

标签 reactjs react-native svg onpress react-native-svg

我正在使用 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>

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

最佳答案

问题是由 panResponder 引起的,因为它比按钮具有更高的优先级,因此解决方案是不要从 onMoveShouldSetPanResponderCapture 返回 always 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;
    }

关于reactjs - react-native-svg onPress 事件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53751467/

相关文章:

reactjs - 如何保持 TypeScript 类型干燥

svg - 如何在react-native中渲染svg?

javascript - 使用 onChangeText 从 React-native 中的嵌套数组中过滤每个值

javascript - 我可以将音频/视频对象从 expo-av 传递到 redux 状态吗?

php - 使用 ImageMagic 将 SVG 转换为 PNG 会忽略填充图案 url

javascript - 有什么方法可以使用户上传的 SVG 图像免受代码注入(inject)等的影响?

javascript - 使用 V8 而不是 JavascriptCore 进行 React-Native

javascript - 在单页 React 应用程序中使用 Driftbot

javascript - 默认导出后 React 组件为 null

javascript - 在 Vanilla JavaScript 中将 SVG 椭圆转换为图像