#鸿蒙通关秘籍#ArkUI的显式动画怎么玩,有人能透露点技巧吗?

HarmonyOS
2024-12-02 10:11:50
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
SaaS古意盎然

怎么玩啊,你可以通过 @ohpm.animate 动画模块进行动画管理,我这边写一个显示动画的玩法代码

@Entry
@Component
struct AnimateToExample {
@State widthSize: number = 250
@State heightSize: number = 100
@State rotateAngle: number = 0
private flag: boolean = true

build() {
Column() {
Button('change size')
.width(this.widthSize)
.height(this.heightSize)
.margin(30)
.onClick(() => {
if (this.flag) {
this.animateButtonSize({
duration: 2000,
curve: Curve.EaseOut,
iterations: 3,
playMode: PlayMode.Normal,
onFinish: () => {
console.info('play end')
}
})
} else {
this.animateButtonSize({})
}
this.flag =!this.flag
})
Button('stop rotating')
.margin(50)
.rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })
.onAppear(() => {
this.startRotateAnimation()
})
.onClick(() => {
this.stopRotateAnimation()
})
}.width('100%').margin({ top: 5 })
}

// 自定义动画方法
animateButtonSize(options: AnimateToOptions, callback?: () => void) {
this.getUIContext()?.animateTo(options, () => {
if (this.flag) {
this.widthSize = 150
this.heightSize = 60
} else {
this.widthSize = 250
this.heightSize = 100
}
callback?.();
});
}

// 开始旋转动画
startRotateAnimation() {
this.getUIContext()?.animateTo({
duration: 1200,
curve: Curve.Friction,
delay: 500,
iterations: -1,
playMode: PlayMode.Alternate,
expectedFrameRateRange: {
min: 10,
max: 120,
expected: 60,
}
}, () => {
this.rotateAngle = 90
});
}

// 停止旋转动画
stopRotateAnimation() {
this.getUIContext()?.animateTo({ duration: 0 }, () => {
this.rotateAngle = 0
});
}
}
  • 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.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
已于2024-12-19 18:40:59修改
分享
微博
QQ
微信
回复
2024-12-02 12:03:58
相关问题
动画请求绘制帧率
984浏览 • 1回复 待解决