目录

Progress如何控制平滑过渡的速率

问题现象

如何通过Progress实现进度条功能,且可以控制其过渡速率?

效果预览

背景知识

解决方案

为实现进度条功能,可通过修改Progress的value值来实现,若要实现控制其过渡速率可将Progress的enableSmoothEffect属性设置为false,关闭进度平滑动效,通过animateTo来设置其过渡动效。

@Entry
@Component
struct WidgetsProgress {
  @State proValue: number = 0;

  build() {
    Column() {
      Progress({ value: this.proValue, type: ProgressType.Linear })
        .width(300)
        .style({ strokeWidth: 10, enableSmoothEffect: false });
      // 当进度达到100%时显示“加载完成”文本
      Text('加载完成').visibility(this.proValue >= 100 ? Visibility.Visible : Visibility.None);
      Button('进度条++')
        .margin({ top: 5, bottom: 5 })
        .onClick(() => {
          this.getUIContext().animateTo({
            duration: 1000,
            curve: Curve.Linear,
            playMode: PlayMode.Normal
          }, () => {
            this.proValue += 10;
          });
        });
      Button('进度条重置')
        .onClick(() => {
          // 重置进度值为0
          this.proValue = 0;
        });
    }
    .width('100%')
    .margin({ top: 50 });
  }
}