使用Progress组件实现其上下移动时,可以动态修改Progress组件的宽高值的效果,但移动过程中Progress组件的宽高值并未发生变化。
问题代码示例参考如下:
@Entry @Component struct Index { @State cameraTranslateY: number = 0 @State cameraBtnSize: number = 65 @State isTranslate: boolean = false // 是否已经向下平移 aboutToAppear(): void { } clickEvent() { if (this.isTranslate) { this.getUIContext().animateTo({ duration: 6000, iterations: 1, playMode: PlayMode.Alternate }, () => { this.cameraTranslateY = 0 this.cameraBtnSize = 65 }) } else { this.getUIContext().animateTo({ duration: 6000, iterations: 1, playMode: PlayMode.Alternate }, () => { this.cameraTranslateY = 100 this.cameraBtnSize = 50 }) } this.isTranslate = !this.isTranslate } build() { RelativeContainer() { Text('点击Progress组件即可看到效果') .alignRules({ top: { anchor: '__container__', align: VerticalAlign.Top }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .margin({ top: 60 }) Row() { Progress({ value: 50, total: 100, type: ProgressType.Ring }) .width(this.cameraBtnSize + 30) .aspectRatio(1) .color('#FFA04F') .backgroundColor('#F3F3F3') .style({ strokeWidth: 3 }) .padding(15) .onClick(() => { this.clickEvent() }) } .width(this.cameraBtnSize + 30) .aspectRatio(1) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .backgroundColor(Color.Grey) .translate({ y: this.cameraTranslateY }) } .width('100%') .height('100%') .backgroundColor(Color.White) } }
问题效果预览:

使用@ohos.animator(动画)和显式动画(animateTo)创建并配置动画,通过translate设置页面转场时的平移效果,最后使用点击事件控制动画的播放形式从而实现Progress组件动态变化效果。
import { AnimatorResult } from '@kit.ArkUI'; @Entry @Component struct ProgressDemo { @State cameraTranslateY: number = 0; @State cameraBtnSize: number = 65; @State isTranslate: boolean = false; // 是否已经向下平移 private backAnimator: AnimatorResult | undefined = undefined; // 创建动画 create() { this.backAnimator = this.getUIContext().createAnimator({ duration: 6000, easing: 'ease', delay: 0, fill: 'forwards', direction: 'normal', iterations: 1, begin: 100, end: 50 }); this.backAnimator.onFinish = () => { console.info('backAnimator onfinish'); }; this.backAnimator.onRepeat = () => { console.info('backAnimator repeat'); }; this.backAnimator.onCancel = () => { console.info('backAnimator cancel'); }; this.backAnimator.onFrame = (value: number) => { this.cameraBtnSize = value; }; }; // 页面即将出现时调用 aboutToAppear(): void { this.create(); }; build() { RelativeContainer() { Text('点击Progress组件即可看到效果') .alignRules({ top: { anchor: '__container__', align: VerticalAlign.Top }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .margin({ top: 60 }); Row() { Progress({ value: 50, total: 100, type: ProgressType.Ring }) .width(this.cameraBtnSize + 30) .aspectRatio(1) // 设置宽高比 .color('#FFA04F') .backgroundColor('#F3F3F3') .style({ strokeWidth: 3 }) .padding(15) .onClick(() => { // 点击事件处理 this.isTranslate = !this.isTranslate; if (this.isTranslate) { this.getUIContext().animateTo({ duration: 6000, iterations: 1, playMode: PlayMode.Alternate }, () => { this.cameraTranslateY = 100; }); this.backAnimator?.play(); } else { this.getUIContext().animateTo({ duration: 6000, iterations: 1, playMode: PlayMode.Alternate }, () => { this.cameraTranslateY = 0; }); this.backAnimator?.reverse(); } }); } .width(this.cameraBtnSize + 30) .aspectRatio(1) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .backgroundColor(Color.Grey) .translate({ y: this.cameraTranslateY }) } .width('100%') .height('100%') .backgroundColor(Color.White) } }