【问题标题】:How do I track the currently-touched element while the touch point moves between multiple touchable elements?当触摸点在多个可触摸元素之间移动时,如何跟踪当前触摸的元素?
【发布时间】:2020-08-28 20:11:32
【问题描述】:

我有这样的用户界面:

<Container>
  <Box id="a" />
  <Box id="b" />
  <Box id="c" />
</Container>
<Text>You are touching: {touchedBoxId}</Text>
 ______________
|container     |
|  __  __  __  |
| |a ||b ||c | | You are touching: a
| |__||__||__| |
|______________|

我想在触摸不同元素时更新You are touching: a 文本。具体来说,我想支持这种用法:

  • 用一根手指在容器中的任意位置按下
  • 在将手指保持在屏幕上的同时,将手指移动到任意框a/b/c
  • 当手指触摸a/b/c时,更新文本
  • 将手指移到另一个框并再次更新文本

我尝试将onPressIn 处理程序添加到每个框。这适用于点击单个框。但是,如果我在框之间移动手指,则不会调用其他框的 onPressIn 处理程序。换句话说,我必须在另一个盒子观察到触摸事件之前抬起手指。

我之前通过将PanResponder 处理程序添加到Container 并根据移动事件的坐标确定哪个框被触摸来实现这一点。这种方法很麻烦,我假设有一种更简单的方法来实现所需的行为。

如果重要的话,容器实际上是 react-native-svg Svg 元素,而框 a/b/c 确实是 SVG Rect 元素。

我该如何实现?

【问题讨论】:

    标签: react-native react-native-svg panresponder


    【解决方案1】:

    使用 onResponderMove(event, gestureState),同时触摸 svg 元素 A 并将手指移动到元素 B。我们可以直接处理元素A,但不能处理B。在onResponderMove 中,使用 (event.nativeEvent.locationX &gt;= B.x &amp;&amp; event.nativeEvent.locationX &lt;= (B.x + b.width) &amp;&amp; event.nativeEvent.locationY &gt;= B.y &amp;&amp; event.nativeEvent.locationY &lt;= (B.y + B.height))

    如果您找到了执行上述操作的更好方法,请告诉我。 或者使用 react-native-gesture-handler 中的 PanGestureHandler。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2020-04-10
      • 1970-01-01
      • 2012-11-03
      相关资源
      最近更新 更多