[英]How do I track the currently-touched element while the touch point moves between multiple touchable elements?
I have a UI like this:我有一个这样的用户界面:
<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
| |__||__||__| |
|______________|
I want to update the You are touching: a
text when a different element is touched.我想更新
You are touching: a
不同元素时的文本。 Specifically, I want to support this usage:具体来说,我想支持这种用法:
a/b/c
a/b/c
框a/b/c
, update the texta/b/c
,更新文本 I tried adding onPressIn
handlers to each box.我尝试向每个框添加
onPressIn
处理程序。 This works for tapping a single box.这适用于点击单个框。 However, if I move my finger between boxes, then the other boxes'
onPressIn
handlers are not called.但是,如果我在框之间移动手指,则不会调用其他框的
onPressIn
处理程序。 In other words, I have to lift my finger before another box will observe a touch event.换句话说,在另一个盒子观察到触摸事件之前,我必须抬起我的手指。
I have implemented this before by adding PanResponder
handlers to Container
and figuring out which box is touched based on move events' coordinates.我之前通过将
PanResponder
处理程序添加到Container
并根据移动事件的坐标确定触摸了哪个框来实现这一点。 This approach is cumbersome, and I assume there is a simpler way to implement the desired behavior.这种方法很麻烦,我认为有一种更简单的方法来实现所需的行为。
If it matters, the container is really a react-native-svg
Svg
element and the boxes a/b/c
are really SVG Rect
elements.如果重要的话,容器实际上是一个
react-native-svg
Svg
元素,而框a/b/c
是真正的 SVG Rect
元素。
How can I implement this?我该如何实施?
Using onResponderMove(event, gestureState), while touched svg element A
and moved finger to element B
.使用 onResponderMove(event,gestureState),同时触摸 svg 元素
A
并将手指移动到元素B
。 We can straight away handle element A
but not B
.我们可以直接处理元素
A
而不是B
。 In onResponderMove
, use (event.nativeEvent.locationX >= Bx && event.nativeEvent.locationX <= (Bx + b.width) && event.nativeEvent.locationY >= By && event.nativeEvent.locationY <= (By + B.height))
在
onResponderMove
,使用(event.nativeEvent.locationX >= Bx && event.nativeEvent.locationX <= (Bx + b.width) && event.nativeEvent.locationY >= By && event.nativeEvent.locationY <= (By + B.height))
Do let me know if you found a better way of doing the above.如果您找到了更好的方法来完成上述操作,请告诉我。 Or use PanGestureHandler from react-native-gesture-handler.
或者使用 react-native-gesture-handler 中的 PanGestureHandler。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.