最近在項目中遇到一個需求,就是需要在微信小程序做一個持續(xù)旋轉(zhuǎn)的動作,在開發(fā)過程中發(fā)現(xiàn)了一些問題,比如如何持續(xù)旋轉(zhuǎn),計時器為什么清除了還是會快速旋轉(zhuǎn)動畫。
本來最先考慮用關鍵幀做動畫,但是在編譯的過程中出錯了,所以最終使用了wx.createAnimation來實現(xiàn),下面貼下代碼(本代碼是基于mpvue寫的),希望對遇到同樣坑的人有點幫助:
onShow () {
this.animate = wx.createAnimation({
duration: 1000
})
const that = this
if (that.timer) {
clearInterval(that.timer)
}
that.timer = setInterval(function () {
that.n = that.n + 1
that.animate.rotateY(45 * that.n).step()
that.myRotate = that.animate.export()
}, 1000)
},
onUnload () {
this.n = 0
clearInterval(this.timer)
this.animate.rotateY(0).step()
this.myRotate = this.animate.export()
},
重點就在onUnload里面,退出頁面清楚定時器,還需要重置動畫。官方?jīng)]說清楚,搞的我加班到現(xiàn)在。。。。呵,程序員~~~