HarmonyOS Grid 删除元素时,如何添加动效

使用Grid布局时,触发了删除操作,如何实现带动效的删除

期望效果:

当用户点击“取消订阅”后,可见选中的元素「在动画过程中」消失,后面的元素也是伴随动画的逐渐向前移动。

遇到的问题:

当前在HarmonyOS开发过程中,发现删除元素后,元素只是原地消失,也没有后面元素前移的效果。

查阅文档未找到相关API

麻烦提供下思路、文档或其他解决方案,以便满足删除元素动画效果。

对比其他系统,实现该效果的方式为:

self.collectionView.performBatchUpdates { [weak self] in
  guard let self = self else {
    return
  }
  self.collectionView.deleteItems(at: [IndexPath(item: selectIndex, section: 0)])
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
HarmonyOS
2025-01-09 15:14:55
443浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

可以参考一下demo:

@Entry
@Component
struct GridObjectSortComponentItem {
  @State numbers: String[] = ["娱乐", "关注", "热榜", "航天", "动漫", "宠物", "保险", "NBA", "汽车", "财经", "体育", "女性", "搞笑", "军事", "有料", "北京", "深度"]
  scroller: Scroller = new Scroller()
  @State text: string = 'drag'
  @State isShowDelete: boolean = false
  @State isEdit: boolean = false
  @State rotateZ: number = 0;

  private stopJump() {
    animateTo({
      delay: 0,
      tempo: 5,
      duration: 0,
      curve: Curve.Smooth,
      playMode: PlayMode.Normal,
      iterations: 1
    }, () => {
      this.rotateZ = 0;
    })
  }
  //抖动的动画
  private jumpWithSpeed(speed: number) {
    if (this.isEdit) {
      this.rotateZ = -1;
      animateTo({
        delay: 0,
        tempo: speed,
        duration: 1000,
        curve: Curve.Smooth,
        playMode: PlayMode.Normal,
        iterations: -1
      }, () => {
        this.rotateZ = 1;
      })
    } else {
      this.stopJump()
    }
  }
  //拖拽过程中展示的样式
  @Builder
  pixelMapBuilder() {
    Column() {
      Text(this.text)
        .fontSize(16)
        .backgroundColor(0xF9CF93)
        .width(80)
        .height(40)
        .textAlign(TextAlign.Center)
        .borderRadius(20)
        .borderWidth(2)
        .borderColor(Color.Orange)
    }
  }

  //改变数组中元素位置
  changeIndex(index1: number, index2: number) {
    this.numbers.splice(index2, 0, this.numbers.splice(index1, 1)[0])
  }

  build() {
    Column({ space: 5 }) {
      Row({ space: 30 }) {
        Text("我的频道")
          .fontSize(20)
        Text(this.isEdit ? "长按拖动调整顺序" : "点击编辑按钮进入编辑")
          .fontColor(Color.Gray)
        Button() {
          Text(this.isEdit ? "完成" : "编辑")
            .fontColor(this.isEdit ? Color.Black : 0xF9CF93)
        }.width(80)
        .height(30)
        .backgroundColor(Color.White)
        .onClick(() => {
          this.isEdit = !this.isEdit
          this.jumpWithSpeed(5)
        })
      }.padding({ left: 15 })

      Grid(this.scroller) {
        ForEach(this.numbers, (day: string, index: number) => {
          GridItem() {
            Stack({ alignContent: Alignment.TopEnd }) {
              Text(day)
                .borderRadius(0)
                .borderWidth(2)
                .borderColor(Color.Orange)
                .fontSize(16)
                .backgroundColor(0xF9CF93)
                .width(80)
                .height(40)
                .textAlign(TextAlign.Center)
                .onTouch((event: TouchEvent) => {
                  if (event.type === TouchType.Down) {
                    this.text = day
                  }
                })
              if (this.isEdit) {
                Image($r('app.media.startIcon'))
                  .width(20)
                  .height(20)
                  .onClick(() => {
                    animateTo({ duration: 300 }, () => {
                      this.numbers.splice(index, 1)
                    })
                    this.stopJump()
                    this.jumpWithSpeed(5)
                  })
              }
            }
            .rotate({
              z: this.rotateZ,
              angle: 0.4,
              centerX: 0.5,
              centerY: 0.5
            })
          }
          .transition({ type: TransitionType.All, translate: { x: 100 }, scale: { x: 1, y: 1 } })
          .padding({ top: 15 })
        })
      }
      .columnsTemplate('1fr 1fr 1fr')
      .columnsGap(10)
      .rowsGap(10)
      .onScrollIndex((first: number) => {
        console.info(first.toString())
      })
      .margin({ top: 5 })
      .width('100%')
      .backgroundColor(0xFAEEE0)
      .height('100%')
      .supportAnimation(true)
      //设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem
      .editMode(this.isEdit)
      //第一次拖拽此事件绑定的组件时,触发回调
      .onItemDragStart((event: ItemDragInfo, itemIndex: number) => {
        //设置拖拽过程中显示的图片
        return this.pixelMapBuilder()
      })
      //绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调
      //itemIndex为拖拽起始位置,insertIndex为拖拽插入位置
      .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => {
        //不支持拖拽到已有内容以外的位置
        if (insertIndex < this.numbers.length) {
          this.changeIndex(itemIndex, insertIndex)
          this.stopJump()
          this.jumpWithSpeed(5)
        }
      })
    }.width('100%')
    .margin({ top: 5 })
    .alignItems(HorizontalAlign.End)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
分享
微博
QQ
微信
回复
2025-01-09 17:51:16


相关问题
HarmonyOS 属性如何打断
556浏览 • 1回复 待解决
HarmonyOS SVGA图片加载
736浏览 • 1回复 待解决
如何设置全屏返回的
2371浏览 • 1回复 待解决
HarmonyOS NavPathStack如何删除元素
536浏览 • 1回复 待解决
鸿蒙jsUi如何制作按钮按下
9238浏览 • 3回复 待解决
HarmonyOS Grid拖动+删除
643浏览 • 0回复 待解决
HarmonyOS 是否有办法实现SVGA播放
567浏览 • 1回复 待解决
HarmonyOS grid拖拽效果如何添加动画
740浏览 • 1回复 待解决