HarmonyOS 自定义控件的aboutToAppear生命周期里执行animateTo动画没有效果

大致的原因推测可能是因为aboutToAppear在build之前,目前在aboutToAppear中加setTimeout可以解决该问题,setTimeout感觉无法完全保证将动画执行放到build后,想咨询下有没有更优的解决方案。

@Component  
export struct Block {  
  @Prop blockWidth: Length = 40  
  @Prop blockHeight: Length = 40  
  @Prop needDynamics: boolean = true  
  @Prop blockRadius: Resource | number = $r('app.float.radiusS')  
  @State opacityValue: number = this.needDynamics ? 0.4: 1  
  
  startAnimation () {  
    if (!this.needDynamics) return  
    animateTo({  
      playMode: PlayMode.Alternate,  
      iterations: -1,  
      duration: 1000,  
    }, () => {  
      this.opacityValue = 1  
    })  
  }  
  
  aboutToAppear(): void {  
    // 不加setTimeout动画无效,setTimeout感觉无法完全保证将动画执行放到build后  
    setTimeout(() => {  
      this.startAnimation()  
    }, 0)  
  }  
  
  build() {  
    Column()  
      .width(this.blockWidth)  
      .height(this.blockHeight)  
      .borderRadius(this.blockRadius)  
      .backgroundColor($r('app.color.fill2'))  
      .opacity(this.opacityValue)  
  }  
}
  • 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.
HarmonyOS
2024-10-29 11:10:05
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

​可以使用onAppear方法放在组件上,组件完成显示的时候会调用。参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-events-show-hide-V5#示例

分享
微博
QQ
微信
回复
2024-10-29 17:39:10


相关问题
HarmonyOS 自定义组件生命周期
870浏览 • 1回复 待解决
HarmonyOS 自定义生命周期问题
713浏览 • 1回复 待解决
HarmonyOS 生命周期区别
963浏览 • 1回复 待解决