微信小程式把玩(四十)animation API
阿新 • • 發佈:2019-02-19
動畫水還是比較深的,這裡只是簡單介紹下小程式中動畫的一些屬性和注意事項,做動畫前一定要整理好思路將動畫一步步分解,再進行組合!這裡只做引入。
wx.createAnimation(object)
看官方介紹
1.建立一個動畫例項animation。呼叫例項的方法來描述動畫。最後通過動畫例項的export方法匯出動畫資料傳遞給元件的animation屬性。
2.呼叫動畫操作方法後要呼叫 step() 來表示一組動畫完成,可以在一組動畫中呼叫任意多個動畫方法,一組動畫中的所有動畫會同時開始,一組動畫完成後才會進行下一組動畫。step 可以傳入一個跟 wx.createAnimation() 一樣的配置引數用於指定當前組動畫的屬性
這還是比較好理解的比如第一條對應程式碼animation: this.animation.export()
第二條比如縮放動畫,也就說是一組scale,scaleX, scaleY…為一縮放動畫組的一個動畫方法,縮放動畫組和旋轉動畫組通過step()連結,按順序執行。程式碼中體驗吧!看效果反過來看會更容易理解
主要屬性:
這裡主要樹下timingFunction和transformOrigin
timingFunction 設定動畫效果
- linear 預設為linear 動畫一直較為均勻
- ease 開始時緩慢中間加速到快結束時減速
- ease-in 開始的時候緩慢
- ease-in-out 開始和結束時減速
- ease-out 結束時減速
- step-start 動畫一開始就跳到 100% 直到動畫持續時間結束 一閃而過
- step-end 保持 0% 的樣式直到動畫持續時間結束 一閃而過
transformOrigin 設定動畫的基點 預設%50 %50 0
- left,center right是水平方向取值,對應的百分值為left=0%;center=50%;right=100%
- top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%
動畫組及動畫方法
樣式:
旋轉:
縮放:
偏移:
傾斜:
矩陣變形:
演示單個動畫組效果
wxml
<view class="container">
<view animation="{{animation}}" class="view">我在做動畫</view>
</view>
<button type="primary" bindtap="rotate">旋轉</button>
js
Page({
data:{
text:"Page animation",
animation: ''
},
onLoad:function(options){
// 頁面初始化 options為頁面跳轉所帶來的引數
},
onReady:function(){
// 頁面渲染完成
//例項化一個動畫
this.animation = wx.createAnimation({
// 動畫持續時間,單位ms,預設值 400
duration: 1000,
/**
* http://cubic-bezier.com/#0,0,.58,1
* linear 動畫一直較為均勻
* ease 從勻速到加速在到勻速
* ease-in 緩慢到勻速
* ease-in-out 從緩慢到勻速再到緩慢
*
* http://www.tuicool.com/articles/neqMVr
* step-start 動畫一開始就跳到 100% 直到動畫持續時間結束 一閃而過
* step-end 保持 0% 的樣式直到動畫持續時間結束 一閃而過
*/
timingFunction: 'linear',
// 延遲多長時間開始
delay: 100,
/**
* 以什麼為基點做動畫 效果自己演示
* left,center right是水平方向取值,對應的百分值為left=0%;center=50%;right=100%
* top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%
*/
transformOrigin: 'left top 0',
success: function(res) {
console.log(res)
}
})
},
/**
* 旋轉
*/
rotate: function() {
//順時針旋轉10度
//
this.animation.rotate(150).step()
this.setData({
//輸出動畫
animation: this.animation.export()
})
},
onShow:function(){
// 頁面顯示
},
onHide:function(){
// 頁面隱藏
},
onUnload:function(){
// 頁面關閉
}
})
演示多個動畫組效果
這裡我們只需要更改以下程式碼即可
/**
* 旋轉
*/
rotate: function() {
//兩個動畫組 一定要以step()結尾
/**
* 動畫順序 順時針旋轉150度>x,y 放大二倍>x,y平移10px>x,y順時針傾斜>改變樣式和設定寬度寬度
*/
this.animation.rotate(150).step().scale(2).step().translate(10).step().skew(10).step().opacity(0.5).width(10).step({ducation: 8000})
this.setData({
//輸出動畫
animation: this.animation.export()
})
}