HarmonyOS鸿蒙Next中Grid拖拽生成的item会偏移
HarmonyOS鸿蒙Next中Grid拖拽生成的item会偏移 长按item进行拖拽,如果手指不是安在item的中间,拖动的时候item或自动向左或者向右偏移
3 回复
可以使用list拖拽,就不会出现上述问题
参考demo:
import curves from '@ohos.curves';
import Curves from '@ohos.curves'
// xxx.ets
@Entry
@Component
struct ListItemExample {
@State private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@State dragItem: number = -1
@State scaleItem: number = -1
@State neighborItem: number = -1
@State neighborScale: number = -1
private dragRefOffset: number = 0
@State offsetX: number = 0
@State offsetY: number = 0
private ITEM_INTV: number = 120
scaleSelect(item: number): number {
if (this.scaleItem == item) {
return 1.05
} else if (this.neighborItem == item) {
return this.neighborScale
} else {
return 1
}
}
itemMove(index: number, newIndex: number): void {
let tmp = this.arr.splice(index, 1)
this.arr.splice(newIndex, 0, tmp[0])
}
build() {
Stack() {
List({ space: 20, initialIndex: 0 }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('' + item)
.width('100%')
.height(100)
.fontSize(16)
.textAlign(TextAlign.Center)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
.shadow(this.scaleItem == item ? { radius: 70, color: '#15000000', offsetX: 0, offsetY: 0 } :
{ radius: 0, color: '#15000000', offsetX: 0, offsetY: 0 })
.animation({ curve: Curve.Sharp, duration: 300 })
}
.margin({ left: 12, right: 12 })
.scale({ x: this.scaleSelect(item), y: this.scaleSelect(item) })
.zIndex(this.dragItem == item ? 1 : 0)
.translate(this.dragItem == item ? { y: this.offsetY } : { y: 0 })
.gesture(
// 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
GestureGroup(GestureMode.Sequence,
LongPressGesture({ repeat: true })
.onAction((event?: GestureEvent) => {
animateTo({ curve: Curve.Friction, duration: 300 }, () => {
this.scaleItem = item
})
})
.onActionEnd(() => {
animateTo({ curve: Curve.Friction, duration: 300 }, () => {
this.scaleItem = -1
})
}),
PanGesture({ fingers: 1, direction: null, distance: 0 })
.onActionStart(() => {
this.dragItem = item
this.dragRefOffset = 0
})
.onActionUpdate((event: GestureEvent) => {
this.offsetY = event.offsetY - this.dragRefOffset
// console.log('Y:' + this.offsetY.toString())
this.neighborItem = -1
let index = this.arr.indexOf(item)
let curveValue = Curves.initCurve(Curve.Sharp)
let value: number = 0
//根据位移计算相邻项的缩放
if (this.offsetY < 0) {
value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
this.neighborItem = this.arr[index-1]
this.neighborScale = 1 - value / 20;
console.log('neighborScale:' + this.neighborScale.toString())
} else if (this.offsetY > 0) {
value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
this.neighborItem = this.arr[index+1]
this.neighborScale = 1 - value / 20;
}
//根据位移交换排序
if (this.offsetY > this.ITEM_INTV / 2) {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.offsetY -= this.ITEM_INTV
this.dragRefOffset += this.ITEM_INTV
this.itemMove(index, index + 1)
})
} else if (this.offsetY < -this.ITEM_INTV / 2) {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.offsetY += this.ITEM_INTV
this.dragRefOffset -= this.ITEM_INTV
this.itemMove(index, index - 1)
})
}
})
.onActionEnd((event: GestureEvent) => {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
this.neighborItem = -1
})
animateTo({
curve: curves.interpolatingSpring(14, 1, 170, 17),
delay: 150
}, () => {
this.scaleItem = -1
})
})
.onCancel(() => {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
this.neighborItem = -1
})
animateTo({
curve: curves.interpolatingSpring(14, 1, 170, 17),
delay: 150
}, () => {
this.scaleItem = -1
})
})
)
)
}, (item: number) => item.toString())
}
}
.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
}
}
更多关于HarmonyOS鸿蒙Next中Grid拖拽生成的item会偏移的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,Grid拖拽生成的item出现偏移问题,可能由以下原因导致:
-
布局计算问题:Grid布局在拖拽过程中可能未能正确计算item的位置,导致偏移。需检查布局计算逻辑,确保在拖拽时正确更新item坐标。
-
事件处理问题:拖拽事件处理中,可能未正确获取或计算拖拽偏移量。需确保事件处理逻辑准确,拖拽偏移量计算正确。
-
动画效果影响:拖拽过程中可能存在动画效果未正确同步,导致视觉偏移。需检查动画实现,确保拖拽与动画同步。
-
坐标系转换问题:不同组件间坐标系转换可能未正确进行,导致偏移。需确保坐标系转换准确,拖拽位置与预期一致。
解决方案包括检查布局计算、事件处理、动画同步及坐标系转换,确保逻辑准确。
在HarmonyOS鸿蒙Next中,Grid拖拽生成的item出现偏移问题,可能是由于布局计算或事件处理不当引起的。建议检查以下几点:
- 布局计算:确保Grid的布局参数正确,特别是
layout_width
和layout_height
属性。 - 事件处理:在拖拽事件中,检查坐标计算是否准确,确保
onDragEvent
中的x
和y
值正确。 - 动画效果:如果使用了动画,检查动画的起始和结束位置是否与预期一致。
- 调试工具:使用DevEco Studio的布局调试工具,实时查看布局变化,定位问题。
通过这些步骤,可以有效解决Grid拖拽item偏移的问题。