微信小程序把玩(四十)animation API

動畫水還是比較深的,這裏只是簡單介紹下小程序中動畫的一些屬性和注意事項,做動畫前一定要整理好思路將動畫一步步分解,再進行組合!這裏只做引入。

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()
    })
  }
發佈了154 篇原創文章 · 獲贊 173 · 訪問量 132萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章