HarmonyOS鸿蒙Next中onDragStart拖拽疑问
HarmonyOS鸿蒙Next中onDragStart拖拽疑问 onDragStart拖拽疑问:
- 长按拖拽内容默认会有放大效果能否去掉,
- 拖拽时可任意方向拖拽,可否控制,
- 是否有可参考拖拽列表排序的例子。
列表拖拽排序可使用组合手势`LongPressGesture`长按和`PanGesture`拖动手势事件来实现。
1. 此方法默认无缩放效果
2. 可以控制方向,设置`PanGesture`的direction参数;参考文档:`https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-gestures-pangesture-V5`
3. 参考demo:
```javascript
import curves from '[@ohos](/user/ohos).curves';
import Curves from '[@ohos](/user/ohos).curves'
[@Entry](/user/Entry)
[@Component](/user/Component)
struct ListItemExample {
[@State](/user/State) private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[@State](/user/State) dragItem: number = -1
[@State](/user/State) scaleItem: number = -1
[@State](/user/State) neighborItem: number = -1
[@State](/user/State) neighborScale: number = -1
private dragRefOffset: number = 0
[@State](/user/State) offsetX: number = 0
[@State](/user/State) offsetY: number = 0
private ITEM_INTV: number = 120
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('50%')
.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 })
.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
this.neighborItem = -1
let index = this.arr.indexOf(item)
let curveValue = Curves.initCurve(Curve.Sharp)
let value: number = 0
//根据位移交换排序
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中onDragStart拖拽疑问的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
基本信息
姓名:张三
职位:软件工程师
简介:资深软件工程师,擅长Java和Python开发。
<div>
<h2>基本信息</h2>
<p><strong>姓名:</strong>张三</p>
<p><strong>职位:</strong>软件工程师</p>
<p><strong>简介:</strong>资深软件工程师,擅长Java和Python开发。</p>
<img src="" alt="头像">
</div>
在HarmonyOS鸿蒙Next中,onDragStart
是用于处理拖拽事件的回调函数。当用户开始拖拽某个元素时,系统会触发onDragStart
事件。开发者可以在这个回调函数中定义拖拽开始时的行为,例如设置拖拽数据、改变元素样式等。
具体来说,onDragStart
通常与DragEvent
对象一起使用。DragEvent
对象包含了与拖拽事件相关的信息,如拖拽的数据、事件类型等。开发者可以通过DragEvent
对象来获取或设置这些信息。
在鸿蒙Next中,onDragStart
的使用场景可能包括但不限于:在拖拽开始时,将拖拽元素的数据保存到DataHub
中,或者在拖拽开始时改变元素的透明度或样式,以提供视觉反馈。
需要注意的是,onDragStart
事件只有在用户通过触摸或鼠标等方式开始拖拽元素时才会触发。如果开发者需要处理其他与拖拽相关的事件,如onDrag
(拖拽过程中)或onDragEnd
(拖拽结束时),也需要相应的回调函数来处理这些事件。
总之,onDragStart
是鸿蒙Next中处理拖拽事件的重要回调函数,开发者可以通过它来实现拖拽开始时的自定义逻辑。