在使用animateTo方法实现循环动画的过程中,如果希望在每次动画播放时通过更新状态变量来刷新UI,可能会遇到UI无法动态响应更新的问题。例如,即使在事件中配置状态变量changeNum每次播放增加1,并设置动画循环播放3次,实际的UI显示仅更新一次,而onFinish的回调中changeNum仅记录1。
@Entry @Component struct AnimateToExample { @State widthSize: number = 250; @State heightSize: number = 100; uiContext: UIContext | undefined = undefined; @State changeNum: number = 0; aboutToAppear() { this.uiContext = this.getUIContext(); if (!this.uiContext) { console.warn('no uiContext'); return; } } playLrc() { this.uiContext?.animateTo({ duration: 2000, curve: Curve.EaseOut, iterations: 3, playMode: PlayMode.Normal, onFinish: () => { console.info('play end'); } }, () => { this.widthSize = 300; this.heightSize = 60; this.changeNum++; }); } build() { Column() { Button(this.changeNum.toString()) .width(this.widthSize) .height(this.heightSize) .margin(30) .onClick(() => { this.playLrc(); }); }.width('100%').margin({ top: 5 }); } }
问题效果预览:

@Entry @Component struct AnimateToExample { @State widthSize: number = 250; @State heightSize: number = 100; uiContext: UIContext | undefined = undefined; @State changeNum: number = 0; aboutToAppear() { this.uiContext = this.getUIContext(); }; playLrc() { // 使用关键帧动画 this.uiContext?.keyframeAnimateTo({ iterations: 1, onFinish: () => { if (this.changeNum % 3 !== 0) { this.playLrc(); } } }, [ { // 第一段关键帧动画 duration: 800, event: () => { this.widthSize = 300; this.heightSize = 60; this.changeNum++; } }, // 第二段关键帧动画 { duration: 500, event: () => { this.widthSize = 250; this.heightSize = 100; } } ]); } build() { Column() { Button(this.changeNum.toString()) .width(this.widthSize) .height(this.heightSize) .margin(30) .onClick(() => { this.playLrc(); }); }.width('100%').margin({ top: 5 }); } }