转场动画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)
  }
}
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
相关问题
如何实现动画转场效果
424浏览 • 1回复 待解决
如何全局设置页面转场动画
365浏览 • 1回复 待解决
ArkUI转场动画可以改颜色吗?
746浏览 • 1回复 待解决
TransitionEffect动画循环播放如何关闭
728浏览 • 1回复 待解决
服务中心添加卡片怎么添加
5166浏览 • 1回复 待解决
转场动画,谁有好的方案吗?
207浏览 • 1回复 待解决
请问如何去掉ability的转场动画
9911浏览 • 2回复 待解决
ForEach循环渲染的过程是什么样的
338浏览 • 1回复 待解决
ForEach什么情况下会触发复用
392浏览 • 1回复 待解决
自定义弹窗自定义转场动画
469浏览 • 1回复 待解决
buttom样式怎么添加
5367浏览 • 2回复 待解决
图片如何添加渐变模糊
814浏览 • 1回复 待解决
TabList可以添加图片么?
6808浏览 • 3回复 待解决
Waterflow怎么添加header能力?
35浏览 • 1回复 待解决