在长按拖拽排序的场景下，如何实现自定义长按拖拽onItemDragStart的开始触发时长
=============================================

在Grid组件中，onItemDragStart事件的默认触发时长为170毫秒，但当前版本不支持直接修改该参数。可以通过自定义Grid，通过设置长按手势LongPressGesture中的duration参数，来实现自定义长按拖拽的开始触发时长，参考代码如下：

import { curves } from '@kit.ArkUI';

@Entry
@Component
struct ReplaceDefaultTimeSetPage {
  _// Element array_
  @State numbers: number\[\] = \[\];
  @State row: number = 4;
  _// Index of the last element in the array_
  @State lastIndex: number = 0;
  @State dragItem: number = -1;
  @State scaleItem: number = -1;
  @State item: number = -1;
  @State offsetX: number = 0;
  @State offsetY: number = 0;
  _// Set grid column count_
  private str: string = '';
  _// Records the offset of the drag starting position_
  private dragRefOffsetx: number = 0;
  private dragRefOffsety: number = 0;
  private FIX\_VP\_X: number = 108;
  private FIX\_VP\_Y: number = 120;

  aboutToAppear() {
    for (let i = 1; i <= 110; i++) {
      this.numbers.push(i);
    }
    this.lastIndex = this.numbers.length - 1;

  _  // Multiple columns_
    for (let i = 0; i < this.row; i++) {
      this.str = this.str + '1fr ';
    }
  }

  itemMove(index: number, newIndex: number): void {
    if (!this.isDraggable(newIndex)) {
      return;
    }
    let tmp = this.numbers.splice(index, 1);
    this.numbers.splice(newIndex, 0, tmp\[0\]);
  }

  _// Slide down_
  down(index: number): void {
   _ // Specify fixed GridItem does not respond to events_
    if (!this.isDraggable(index + this.row)) {
      return;
    }
    this.offsetY -= this.FIX\_VP\_Y;
    this.dragRefOffsety += this.FIX\_VP\_Y;
   _ // Multiple columns_
    this.itemMove(index, index + this.row);
  }

 _ // Slide down (bottom right is empty)_
  down2(index: number): void {
    if (!this.isDraggable(index + 3)) {
      return;
    }
    this.offsetY -= this.FIX\_VP\_Y;
    this.dragRefOffsety += this.FIX\_VP\_Y;
    this.itemMove(index, index + 3);
  }

 _ // Slide up_
  up(index: number): void {
    if (!this.isDraggable(index - this.row)) {
      return;
    }
    this.offsetY += this.FIX\_VP\_Y;
    this.dragRefOffsety -= this.FIX\_VP\_Y;
    this.itemMove(index, index - this.row);
  }

  _// Slide left_
  left(index: number): void {
    if (!this.isDraggable(index - 1)) {
      return;
    }
    this.offsetX += this.FIX\_VP\_X;
    this.dragRefOffsetx -= this.FIX\_VP\_X;
    this.itemMove(index, index - 1);
  }

  _// Slide right_
  right(index: number): void {
    if (!this.isDraggable(index + 1)) {
      return;
    }
    this.offsetX -= this.FIX\_VP\_X;
    this.dragRefOffsetx += this.FIX\_VP\_X;
    this.itemMove(index, index + 1);
  }

 _ // Slide bottom right_
  lowerRight(index: number): void {
    if (!this.isDraggable(index + this.row + 1)) {
      return;
    }
    this.offsetX -= this.FIX\_VP\_X;
    this.dragRefOffsetx += this.FIX\_VP\_X;
    this.offsetY -= this.FIX\_VP\_Y;
    this.dragRefOffsety += this.FIX\_VP\_Y;
    this.itemMove(index, index + this.row + 1);
  }

  _// Slide top right_
  upperRight(index: number): void {
    if (!this.isDraggable(index - (this.row - 1))) {
      return;
    }
    this.offsetX -= this.FIX\_VP\_X;
    this.dragRefOffsetx += this.FIX\_VP\_X;
    this.offsetY += this.FIX\_VP\_Y;
    this.dragRefOffsety -= this.FIX\_VP\_Y;
    this.itemMove(index, index - (this.row - 1));
  }

  _// Slide bottom left_
  lowerLeft(index: number): void {
    if (!this.isDraggable(index + (this.row - 1))) {
      return;
    }
    this.offsetX += this.FIX\_VP\_X;
    this.dragRefOffsetx -= this.FIX\_VP\_X;
    this.offsetY -= this.FIX\_VP\_Y;
    this.dragRefOffsety += this.FIX\_VP\_Y;
    this.itemMove(index, index + (this.row - 1));
  }

  _// Slide top left_
  upperLeft(index: number): void {
    if (!this.isDraggable(index - (this.row + 1))) {
      return;
    }
    this.offsetX += this.FIX\_VP\_X;
    this.dragRefOffsetx -= this.FIX\_VP\_X;
    this.offsetY += this.FIX\_VP\_Y;
    this.dragRefOffsety -= this.FIX\_VP\_Y;
    this.itemMove(index, index - (this.row + 1));
  }

  _// Control whether the element can be moved and sorted by its index_
  isDraggable(index: number): boolean {
    return index >= 0 && index < this.numbers.length;
  }

  build() {
    Column() {
      Grid() {
        ForEach(this.numbers, (item: number) => {
          GridItem() {
            Text(item + '')
              .fontSize(16)
              .width('100%')
              .textAlign(TextAlign.Center)
              .height(100)
              .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
              })
          }
          .onAreaChange((\_oldVal, newVal) => {
            _// Multiple columns_
            this.FIX\_VP\_X = Math.round(newVal.width as number);
            this.FIX\_VP\_Y = Math.round(newVal.height as number);
          })
      _    // Specify fixed GridItem does not respond to events_
          .hitTestBehavior(this.isDraggable(this.numbers.indexOf(item)) ? HitTestMode.Default : HitTestMode.None)
          .scale({ x: this.scaleItem === item ? 1.05 : 1, y: this.scaleItem === item ? 1.05 : 1 })
          .zIndex(this.dragItem === item ? 1 : 0)
          .translate(this.dragItem === item ? { x: this.offsetX, y: this.offsetY } : { x: 0, y: 0 })
          .padding(10)
          .gesture(
            _//The following combined gestures are recognized sequentially. If the long press gesture event is not triggered normally, the drag gesture event will not be triggered._
            GestureGroup(GestureMode.Sequence,
              LongPressGesture({
                repeat: true,
                duration: 50
              }) _ // Control the duration of the long-press event that triggers dragging, defaulting to 500 milliseconds. Setting it to less than 0 reverts to the default value; here it is set to 50 milliseconds_
                .onAction((\_event?: GestureEvent) => {
                  this.getUIContext().animateTo({
                    curve: Curve.Friction,
                    duration: 300
                  }, () => {
                    this.scaleItem = item;
                  });
                })
                .onActionEnd(() => {
                  this.getUIContext().animateTo({
                    curve: Curve.Friction,
                    duration: 300
                  }, () => {
                    this.scaleItem = -1;
                  });
                }),
              PanGesture({
                fingers: 1,
                direction: null,
                distance: 0
              })
                .onActionStart(() => {
                  this.dragItem = item;
                  this.dragRefOffsetx = 0;
                  this.dragRefOffsety = 0;
                })
                .onActionUpdate((event: GestureEvent) => {
                  this.offsetY = event.offsetY - this.dragRefOffsety;
                  this.offsetX = event.offsetX - this.dragRefOffsetx;

                  this.getUIContext().animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                    let index = this.numbers.indexOf(this.dragItem);
                    if (this.offsetY >= this.FIX\_VP\_Y / 2 &&
                      (this.offsetX <= this.FIX\_VP\_X / 2 && this.offsetX >= -this.FIX\_VP\_X / 2)
                      && (index + this.row <= this.lastIndex)) {
                    _  // Slide down_
                      this.down(index);
                    } else if (this.offsetY <= -this.FIX\_VP\_Y / 2 &&
                      (this.offsetX <= this.FIX\_VP\_X / 2 && this.offsetX >= -this.FIX\_VP\_X / 2)
                      && index - this.row >= 0) {
                  _    // Slide up_
                      this.up(index);
                    } else if (this.offsetX >= this.FIX\_VP\_X / 2 &&
                      (this.offsetY <= this.FIX\_VP\_Y / 2 && this.offsetY >= -this.FIX\_VP\_Y / 2)
                      && !(((index - (this.row - 1)) % this.row === 0) || index === this.lastIndex)) {
                   _   // ) {_
                      _// Slide right_
                      this.right(index);
                    } else if (this.offsetX <= -this.FIX\_VP\_X / 2 &&
                      (this.offsetY <= this.FIX\_VP\_Y / 2 && this.offsetY >= -this.FIX\_VP\_Y / 2)
                      && !(index % this.row === 0)) {
                     _ // Slide left_
                      this.left(index);
                    } else if (this.offsetX >= this.FIX\_VP\_X / 2 && this.offsetY >= this.FIX\_VP\_Y / 2
                      && ((index + this.row + 1 <= this.lastIndex && !((index - (this.row - 1)) % this.row === 0)) ||
                        !((index - (this.row - 1)) % this.row === 0))) {
                 _     // Slide bottom right_
                      this.lowerRight(index);
                    } else if (this.offsetX >= this.FIX\_VP\_X / 2 && this.offsetY <= -this.FIX\_VP\_Y / 2
                      && !((index - this.row < 0) || ((index - (this.row - 1)) % this.row === 0))) {
                  _    // Slide top right_
                      this.upperRight(index);
                    } else if (this.offsetX <= -this.FIX\_VP\_X / 2 && this.offsetY >= this.FIX\_VP\_Y / 2
                      && (!(index % this.row === 0) && (index + (this.row - 1) <= this.lastIndex))) {
                     _ // Slide bottom left_
                      this.lowerLeft(index);
                    } else if (this.offsetX <= -this.FIX\_VP\_X / 2 && this.offsetY <= -this.FIX\_VP\_Y / 2
                      && !((index <= this.row - 1) || (index % this.row === 0))) {
                     _ // Slide top left_
                      this.upperLeft(index);
                    } else if (this.offsetX >= this.FIX\_VP\_X / 2 && this.offsetY >= this.FIX\_VP\_Y / 2
                      && (index === this.lastIndex)) {
                    _  // Slide right down (bottom right is empty)_
                      this.down2(index);
                    }
                  });
                })
                .onActionEnd(() => {
                  this.getUIContext().animateTo({
                    curve: curves.interpolatingSpring(0, 1, 400, 38)
                  }, () => {
                    this.dragItem = -1;
                  });
                  this.getUIContext().animateTo({
                    curve: curves.interpolatingSpring(14, 1, 170, 17),
                    delay: 150
                  }, () => {
                    this.scaleItem = -1;
                  });
                })
            )
              .onCancel(() => {
                this.getUIContext().animateTo({
                  curve: curves.interpolatingSpring(0, 1, 400, 38)
                }, () => {
                  this.dragItem = -1;
                });
                this.getUIContext().animateTo({
                  curve: curves.interpolatingSpring(14, 1, 170, 17)
                }, () => {
                  this.scaleItem = -1;
                });
              })
          )
        }, (item: number) => item.toString())
      }
      .width('90%')
      .editMode(true)
      .scrollBar(BarState.Off)
    _  // Multiple columns_
      .columnsTemplate(this.str)
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#0D182431')
    .padding({ top: 5 })
  }
}

效果图如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/3a/v3/2t2pDeqjQEOQ2GFTWm9f3w/zh-cn_image_0000002654795305.png?HW-CC-KV=V1&HW-CC-Date=20260709T133853Z&HW-CC-Expire=86400&HW-CC-Sign=F09D6D773647042840ECA5C975FA5B86012534CEAB4E1777D2BB4E8357510EE4 "点击放大")