onDragStart拖拽疑问,求高手指点?

onDragStart拖拽疑问:

1、长按拖拽内容默认会有放大效果能否去掉,

2、拖拽时可任意方向拖拽,可否控制,

3、是否有可参考拖拽列表排序的例子。

HarmonyOS
2024-10-14 11:41:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

列表拖拽排序可使用组合手势LongPressGesture长按和PanGesture拖动手势事件来实现。

1、此方法默认无缩放效果

2、可以控制方向,设置PanGesture的direction参数;参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-gestures-pangesture-V5

3、参考demo:

import curves from '@ohos.curves';  
import Curves from '@ohos.curves'  
  
@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  
  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  
                  // 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 > 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 })  
  }  
}
分享
微博
QQ
微信
回复
2024-10-14 15:50:56
相关问题
使用的模拟器,指点
2324浏览 • 2回复 待解决
hpm下载库出错,不能下载,请指点
6344浏览 • 1回复 待解决
拖拽时怎么设置当前拖拽项目数
864浏览 • 1回复 待解决
HarmonyOS 拖拽不起效果
334浏览 • 1回复 待解决
Grid如何实现拖拽功能
2444浏览 • 1回复 待解决
mysql varchar类型的疑问
2212浏览 • 1回复 待解决
HarmonyOS 关于deliveryWithInstall的疑问
503浏览 • 1回复 待解决
HarmonyOS 应用相机开发疑问
86浏览 • 1回复 待解决
HarmonyOS关于RichText的疑问
671浏览 • 1回复 待解决
HarmonyOS image组件拖拽问题
180浏览 • 1回复 待解决
拖拽事件extraParams返回空
212浏览 • 1回复 待解决
HarmonyOS如何实现list listitem拖拽
676浏览 • 1回复 待解决
HarmonyOS 企业证书的疑问
48浏览 • 1回复 待解决
OpenHarmony camera sensor调试疑问
1720浏览 • 0回复 待解决
HarmonyOS 关于手动签名的疑问
369浏览 • 1回复 待解决
grid如何怎么实现拖拽功能
761浏览 • 1回复 待解决
HarmonyOS 悬浮窗拖拽功能怎么处理?
210浏览 • 1回复 待解决