简体   繁体   English

当触摸点在多个可触摸元素之间移动时,如何跟踪当前触摸的元素?

[英]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:具体来说,我想支持这种用法:

  • Press down at any point in the container with one finger用一根手指按下容器中的任何一点
  • While keeping the finger in the screen, move the finger to any box a/b/c在将手指保持在屏幕上的同时,将手指移动到任何a/b/c
  • When the finger touches a/b/c , update the text当手指触摸a/b/c ,更新文本
  • Move the finger to another box and update the text again将手指移动到另一个框并再次更新文本
  • Etc.等等。

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.

相关问题 如何禁用可触摸组件中子元素的触摸 - How do I disable the touch of a child element in Touchable Component 如何嵌套多个可触摸组件? - How do I nest multiple touchable components? 翻译到屏幕上的可触摸元素不注册触摸 - Touchable Elements Translated onto screen do not register touch 如何在 React Native 的嵌套 Touchable 中传播触摸事件? - How can I propagate touch event in nested Touchable in React Native? 如何处理带有样式更改的按下和取消按下可触摸组件? - How do I handle press and unpress touchable component with style changes? 如何检测用户在本机反应中触摸某个元素? - How do I detect the users touch on an element in react native? 如何在本机反应中同时启用多个按钮的触摸? - How do I enable touch on multiple buttons simultaneously in react native? 如何在触摸和移动手指期间找出用户在 React Native 中触摸了特定视图? - how can I find out during a touch and moving a finger, user touched a specific view in react native? 我不能将 flexbox 用于可触摸的不透明度/可按压。 如何连续对齐这两个元素? - Can't I use flexbox for touchable opacity/ pressable. How to align these 2 elements in a row? 如何禁用 react-native-element 的复选框可触摸不透明度? - How to disable react-native-element's checkbox touchable opacity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM