#鸿蒙通关秘籍# 如何使用鸿蒙JS动画接口实现多种交互动画效果,如播放、暂停、更新?

HarmonyOS
2024-12-13 11:50:42
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
ORM幻影舞

在鸿蒙环境下,利用JS动画接口可以实现多种交互动画效果。实现方法如下:

首先,创建动画对象,并设置初始参数。 javascript import animator from '@ohos.animator'; import promptAction from '@ohos.promptAction';

export default { data: { scaleVal: 1, DivWidth: 200, DivHeight: 200, translateVal: 0, animation: null }, onInit() { var options = { duration: 3000, fill: 'forwards', begin: 200, end: 270 }; this.animation = animator.createAnimator(options); }, playAnimation() { var _this = this; this.animation.onframe = function(value) { _this.scaleVal = value / 150; _this.DivWidth = value; _this.DivHeight = value; _this.translateVal = value - 180; }; this.animation.play(); } }

然后,编写HML,添加触发动画事件的按钮。 html <div style="flex-direction: column; align-items: center; width: 100%; height: 100%;"> <div style="width:200px; height: 200px; margin-top: 100px; background: linear-gradient(#b30d29, #dcac1b); transform: scale(`scaleVal`);"></div> <div style="width: `DivWidth`; height: `DivHeight`; margin-top: 200px; background: linear-gradient(pink, purple); margin-top: 200px; transform: translateY(`translateVal`);"></div> <div class="row"> <button type="capsule" value="play" onclick="playAnimation"></button> <button type="capsule" value="update" onclick="updateAnimation"></button> </div> <div class="row1"> <button type="capsule" value="pause" onclick="pauseAnimation"></button> <button type="capsule" value="finish" onclick="finishAnimation"></button> </div> <div class="row2"> <button type="capsule" value="cancel" onclick="cancelAnimation"></button> <button type="capsule" value="reverse" onclick="reverseAnimation"></button> </div> </div>

CSS文件用于按钮排版和定位。 css button { width: 200px; } .row { width: 65%; height: 100px; align-items: center; justify-content: space-between; margin-top: 150px; position: fixed; top: 52%; left: 120px; } .row1 { width: 65%; height: 100px; align-items: center; justify-content: space-between; margin-top: 120px; position: fixed; top: 65%; left: 120px; } .row2 { width: 65%; height: 100px; align-items: center; justify-content: space-between; margin-top: 100px; position: fixed; top: 75%; left: 120px; }

最后,实现动画操作的方法,支持播放、更新、暂停、完成、取消、倒放等交互功能。 javascript updateAnimation() { var newoptions = { duration: 5000, iterations: 2, begin: 120, end: 180 }; this.animation.update(newoptions); this.animation.play(); }, pauseAnimation() { this.animation.pause(); }, finishAnimation() { this.animation.onfinish = function() { promptAction.showToast({ message: 'finish' }) }; this.animation.finish(); }, cancelAnimation() { this.animation.cancel(); }, reverseAnimation() { this.animation.reverse(); }

这样通过鸿蒙JS动画接口,利用按钮事件,可以实现复杂的动画效果,包括各种交互操作。

分享
微博
QQ
微信
回复
2024-12-13 13:23:16
相关问题