ios React-天然:使用panResponder获取可拖动对象的X和Y坐标,panHandlers(nativeElement)

dzhpxtsq  于 2023-01-10  发布在  iOS
关注(0)|答案(2)|浏览(231)

我正在开发一个应用程序,它提供了不同形状的设计,如方形和圆形,我使用了以下库
https://www.npmjs.com/package/react-native-draggable
在创建我的图纸视图时,它工作正常,但在编辑该图纸时,我想将圆和正方形从其先前保存的位置移动到其新位置,但当我试图从DB设置我先前的最后位置时,我无法获得X和Y的完美位置。

调试时,获取的是对象的触摸位置,而不是对象的X和Y位置
这是我的代码:
1.我使用了draggable类来创建对象Draggable
1.在我的组件中:

<Draggable
                       renderSize={68}
                       renderColor='rad'
                       x={80.80097961425781}
                       y={72.79156494140625}
                       renderText='B'
                       pressDrag={()=>alert('touched!!')}
                       pressDragRelease={({ nativeEvent }) => { console.log(nativeEvent);}}
                       reverse={false}
                   />

这是我的问题:
1.如何从nativeElement得到X和Y
"nativeElement"属性为:* * 本机事件**

ChangedTouches - Array of all touch events that have changed since the last event
identifier - The ID of the touch
locationX - The X position of the touch, relative to the element
locationY - The Y position of the touch, relative to the element
pageX - The X position of the touch, relative to the root element
pageY - The Y position of the touch, relative to the root element
target - The node id of the element receiving the touch event
timestamp - A time identifier for the touch, useful for velocity calculation
touches - Array of all current touches on the screen
4ioopgfo

4ioopgfo1#

我在一个项目中也使用了react-native-draggablepageXpageY是你实际触摸的坐标,而locationXlocationY是你触摸到对象左上角的偏移量。你没有指定你如何渲染形状,但如果你渲染一个图像来拖动,你可以通过得到(pageX-locationX),(pageY-locationY)得到左上角的精确坐标,你可以这样把它们从nativeEvent中提取出来:

<Draggable
  renderSize={68}
  renderColor='rad'
  x={80.80097961425781}
  y={72.79156494140625}
  renderText='B'
  onDragRelease={(e) => {console.log("pageX, pageY = " + e.nativeEvent.pageX + ", " + e.nativeEvent.pageY);
  console.log("locX, locY = " + e.nativeEvent.locationX + ", " + e.nativeEvent.locationY)}}>
  reverse={false}
/>

此外,我将pressDragRelease更改为onDragRelease;我不确定你是把onDragRelease封装在自己的函数里了,还是你有一个不同的版本,但是在我的函数里是onDrag/onDragRelease,希望能有所帮助。

weylhg0b

weylhg0b2#

你可以在回调函数的下一个参数中得到Dragable的边界。

<Draggable
  ...
  onDragRelease={(e, gestureState, bounds) => { console.log(bounds); }}
/>

相关问题