#鸿蒙通关秘籍#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
});
}
}
已于2024-12-19 18:40:59修改
分享
微博
QQ
微信
回复
2024-12-02 12:03:58
相关问题
动画请求绘制帧率
409浏览 • 1回复 待解决