PanGesture手势控制onActionUpdate中GestureEvent的localX不准确?在onActionUpdate中如何准确获取组件相对于父组件的xy坐标? HarmonyOS 鸿蒙Next
PanGesture手势控制onActionUpdate中GestureEvent的localX不准确?在onActionUpdate中如何准确获取组件相对于父组件的xy坐标? HarmonyOS 鸿蒙Next
通过PanGesture手势滑动设置组件的position实现手势拖动组件的效果,当点击滑动刚开始时会出现漂移的情况,打印日志发现上一次移动时x停留在179.0700401893029,再次点击移动手指,刚开始x就变成了42.45685049203726
更多关于PanGesture手势控制onActionUpdate中GestureEvent的localX不准确?在onActionUpdate中如何准确获取组件相对于父组件的xy坐标? HarmonyOS 鸿蒙Next的实战教程也可以访问 https://www.itying.com/category-93-b0.html
// 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
GestureGroup(GestureMode.Sequence,
LongPressGesture({ repeat: true })
.onAction((event: GestureEvent) => {
if (event.repeat) {
this.count++
for (let i = 0; i < event.fingerList.length; i++) {
this.lX = event.fingerList[i].localX
this.lY = event.fingerList[i].localY
}
}
console.info('LongPress onAction')
console.log('yeyouzhi lx===' + this.lX)
console.log('yeyouzhi ly===' + this.lY)
})
.onActionEnd(() => {
console.info('LongPress end')
}),
更多关于PanGesture手势控制onActionUpdate中GestureEvent的localX不准确?在onActionUpdate中如何准确获取组件相对于父组件的xy坐标? HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS中,PanGesture
的GestureEvent
对象提供了offsetX
和offsetY
属性,用于获取手势事件相对于父组件的坐标。此外,可以通过getBoundingClientRect
方法获取组件在屏幕上的位置信息,然后进行计算。
例如,假设你有一个Component
对象comp
,你可以通过以下方式获取其相对于父组件的坐标:
let rect = comp.getBoundingClientRect();
let parentRect = comp.parent.getBoundingClientRect();
let relativeX = rect.left - parentRect.left;
let relativeY = rect.top - parentRect.top;
这样,你就可以在onActionUpdate
中准确获取组件相对于父组件的坐标,而不再依赖localX
和localY
。