HarmonyOS鸿蒙Next中animateTo动画如何变更效果!!

HarmonyOS鸿蒙Next中animateTo动画如何变更效果!!

这样执行一个无限循环的动画:

```kotlin
animateTo({
  duration: this.animationTime,
  curve: curves.cubicBezierCurve(0.4, 0, 0.2, 1),
  iterations: -1,
  playMode: PlayMode.Alternate
}, () => {
  this.fontAnimation = 0.9
})

如何不停止动画变更duration 时间,从而变更动画速度效果!

4 回复

开发者您好,参考以下方案看看:

在状态管理V2中,当使用@Param@Monitor修饰的状态变量发生变化时,系统会首先执行@Monitor绑定的方法。由于自定义组件内的build方法还未执行,内部组件还未创建,动画时机过早,导致动画属性没有初值,无法对组件产生动画效果。为了解决该问题,可以为动画添加setTimeout延时,确保内部组件创建完成后再执行动画。

示例代码如下:

// StateV2Page.ets
import { StateV2Loading } from '../views/StateV2Loading';

@Entry
@ComponentV2
struct StateV2Page {
  @Local message: string = '点击触发Loading';
  @Local isLoading: boolean = false;
  [@Param](/user/Param) animationDuration: number = 800;
  @Local loadingRotate: number = 360;

  build() {
    RelativeContainer() {
      Row() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.isLoading = true
          })
        StateV2Loading({ isLoading: this.isLoading })
      }.justifyContent(FlexAlign.Center)
      .width('100%')
      .height('100%')
      .id("row")
      .alignRules({
        center: { anchor: '__container__', align: VerticalAlign.Center },
        middle: { anchor: '__container__', align: HorizontalAlign.Center }
      })
    }
    .height('100%')
    .width('100%')
  }
}
// StateV2Loading.ets
@ComponentV2
export struct StateV2Loading {
  [@Param](/user/Param) isLoading: boolean = false;
  [@Param](/user/Param) animationDuration: number = 800;
  @Local loadingRotate: number = 0;
  [@Monitor](/user/Monitor)('isLoading')
  startAnim(){
    // 在setTimeout内就可执行动画
    setTimeout(() => {
      animateTo({
        duration: this.animationDuration,
        curve: Curve.Linear,
        iterations: -1
      }, () => {
        this.loadingRotate = 360;
      });
    }, 10)
  }

  build() {
    Row() {
      Image($r('app.media.startIcon'))
        .width(16)
        .height(16)
        .rotate({ angle: this.loadingRotate })
        .animation({
          duration: this.animationDuration,
          curve: Curve.Linear,
          iterations: -1
        })
    }
  }
}

若符合要求,请点击采纳,谢谢!

更多关于HarmonyOS鸿蒙Next中animateTo动画如何变更效果!!的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


不是这个意思, 是动画已经执行, 在动画播放的过程中变更动画效果!

animateTo({ duration: this.animationTime, curve: curves.cubicBezierCurve(0.4, 0, 0.2, 1), iterations: -1, playMode: PlayMode.Alternate }, () => { this.fontAnimation = 0.9 })

通过修改 this.animationTime值然后再次执行这个函数没有效果

在HarmonyOS鸿蒙Next中,animateTo动画效果可以通过调整AnimationOption参数进行变更。例如,使用easing属性来改变动画的缓动效果,如Ease.InOutEase.Linear,或者通过duration属性调整动画持续时间。你还可以结合transform属性实现平移、缩放等效果。示例代码如下:

animateTo({
  duration: 1000,
  easing: Ease.InOut,
}, () => {
  // 在此处定义动画目标状态
  this.translateX = 100;
});

通过调整这些参数,可以灵活控制动画的表现形式。

回到顶部