#鸿蒙通关秘籍#如何在HarmonyOS元服务中使用animateTo接口实现无限循环动画?

HarmonyOS
2024-12-05 14:35:18
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
墨雨尘UI

从API version 11开始,animateTo接口在元服务中得到支持,可以通过在animateTo函数中设置iterations为-1实现无限循环动画。以下代码展示了如何在一个按钮点击事件中无限循环旋转角度变化:

@Entry
@Component
struct AnimateToExample {
  @State rotateAngle: number = 0

  build() {
    Button('change rotate angle')
      .margin(50)
      .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })
      .onClick(() => {
        animateTo({
          duration: 1200,
          curve: Curve.Friction,
          delay: 500,
          iterations: -1,
          playMode: PlayMode.Alternate,
          onFinish: () => {
            console.info('play end')
          },
          expectedFrameRateRange: {
            min: 10,
            max: 120,
            expected: 60,
          }
        }, () => {
          this.rotateAngle = 90
        })
      })
  }
}
  • 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.

在这个代码中,通过设置iterations为-1,实现按钮的旋转角度在点击后无限循环。

分享
微博
QQ
微信
回复
2024-12-05 16:20:10
相关问题