转场动画forEach循环添加回调函数添加index为啥全部触发了?

转场动画forEach循环添加回调函数添加index为啥全部触发了?-鸿蒙开发者社区

/**
 * 转场动画(组件)transition
 * 需要配合animateTo才能生效
 *
 */
@Entry
@Component
struct TransitionTest2 {
  @State numbers: Object[] = []
  startNumber: number = 6;

  build() {
    Column({ space: 10 }) {
      Scroll() {
        Column({ space: 10 }) {
          ForEach(this.numbers, (item,index) => {
            // ForEach下的直接组件需配置transition效果
            Row() {
              Text(item.text + item.id)
                .height(40)
                .backgroundColor(Color.Orange).layoutWeight(1)
              // Button('删除').type(ButtonType.Normal).onClick(()=>{
              //   animateTo({},()=>{
              //     this.numbers.splice(index,1)
              //   })
              // })
            }
            .transition({ type: TransitionType.All, translate: { x: 200 }, scale: { x: 0, y: 0 } })
          }, item => item.id)
        }
        .margin(10)
        // .justifyContent(FlexAlign.Start)
        // .alignItems(HorizontalAlign.Center)
        .width("90%").height('100%')
      }.layoutWeight(1).backgroundColor(Color.Green)

      Row(){
        Button('头部添加')
          .onClick(() => {
            animateTo({ duration: 1000 }, () => {
              // 往数组头部插入一个元素,导致ForEach在头部增加对应的组件
              this.numbers.unshift({ text: '文档', id: this.startNumber.toString() });
              this.startNumber++;
            })
          })
        Button('尾部添加')
          .onClick(() => {
            animateTo({ duration: 1000 }, () => {
              // 往数组尾部插入一个元素,导致ForEach在尾部增加对应的组件
              this.numbers.push({ text: '文档', id: this.startNumber.toString() });
              this.startNumber++;
            })
          })
        Button('删除头部')
          .onClick(() => {
            animateTo({ duration: 1000 }, () => {
              // 删除数组的头部元素,导致ForEach删除头部的组件
              this.numbers.shift();
            })
          })
        Button('删除尾部')
          .onClick(() => {
            animateTo({ duration: 1000 }, () => {
              // 删除数组的尾部元素,导致ForEach删除尾部的组件
              this.numbers.pop();
            })
          })
      }
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.Pink)
  }
}
  • 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.
UI
forEach循环
2024-03-13 14:59:58
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Keeog

没太看明白问题是什么

已于2024-3-14 10:06:29修改
分享
微博
QQ
微信
回复
2024-03-13 16:50:33
相关问题
HarmonyOS forEach return退出循环
822浏览 • 1回复 待解决
求告知子窗口如何添加动画
845浏览 • 1回复 待解决
HarmonyOS forEach如何提前终止循环
692浏览 • 1回复 待解决
HarmonyOS Text添加动画效果与预期不符
763浏览 • 1回复 待解决
HarmonyOS grid拖拽效果如何添加动画
901浏览 • 1回复 待解决
HarmonyOS flutter在ohos如何添加系统回
825浏览 • 1回复 待解决
ForEach无法遍历全部数据,是什么原因
2569浏览 • 1回复 待解决
服务中心添加卡片怎么添加
7539浏览 • 1回复 待解决
如何添加内容的添加渐变模糊
717浏览 • 1回复 待解决
HarmonyOS SideBarContainer 转场动画
530浏览 • 1回复 待解决