首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-native-svg onPress事件不工作

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

Stack Overflow用户
提问于 2018-12-13 05:16:01
回答 1查看 1.4K关注 0票数 3

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

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
<Svg
            height="100"
            width="100"
        >
    <Circle
    cx="50%"
    cy="50%"
    r="38%"
    fill="red"
    onPress={() => alert('Press on Circle')}
/>
  </Svg>

但是作为第一个,它不能工作

EN

回答 1

Stack Overflow用户

发布于 2018-12-24 19:08:43

这个问题是由panResponder引起的,因为它比按钮具有更高的优先级,所以解决方案是不从onMoveShouldSetPanResponderCapture返回总是真。例如:

代码语言:javascript
复制
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;
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53751467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档