最近在項(xiàng)目中遇到一個(gè)需求,就是需要在微信小程序做一個(gè)持續(xù)旋轉(zhuǎn)的動作蛹稍,在開發(fā)過程中發(fā)現(xiàn)了一些問題吧黄,比如如何持續(xù)旋轉(zhuǎn),計(jì)時(shí)器為什么清除了還是會快速旋轉(zhuǎn)動畫唆姐。
本來最先考慮用關(guān)鍵幀做動畫拗慨,但是在編譯的過程中出錯(cuò)了,所以最終使用了wx.createAnimation來實(shí)現(xiàn)奉芦,下面貼下代碼(本代碼是基于mpvue寫的)胆描,希望對遇到同樣坑的人有點(diǎn)幫助:
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()
},
重點(diǎn)就在onUnload里面,退出頁面清楚定時(shí)器仗阅,還需要重置動畫昌讲。官方?jīng)]說清楚,搞的我加班到現(xiàn)在减噪。短绸。车吹。。呵醋闭,程序員~~~