Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

微信小程序菜單彈出動畫:用 createAnimation 做旋轉與位移

編輯:關於 Android 程式設計

先看一下成品:點一下右下角的「+」按鈕,三個功能圖示會以旋轉搭配位移的方式散開;再點一次則收回。

整個效果的核心是 wx.createAnimation():它會回傳一個動畫物件,你在上面串接 rotateZ(旋轉)、translate(位移)、opacity(透明度)等屬性,呼叫 step() 產生影格,最後用 export() 匯出成可綁定到 data 的格式。wxml 只要把這個值掛到 animation 屬性上,動畫就會跑起來。

一、index.js

//index.js
// 獲取應用實例
var app = getApp()

Page({
  data: {
    isPopping: false,      // 是否已經彈出
    animationPlus: {},     // 旋轉動畫
    animationcollect: {},  // item 位移、透明度
    animationTranspond: {},
    animationInput: {}
  },

  onLoad: function () {},

  // 點擊切換
  plus: function () {
    if (this.data.isPopping) {
      // 收回動畫
      popp.call(this);
      this.setData({ isPopping: false })
    } else {
      // 彈出動畫
      takeback.call(this);
      this.setData({ isPopping: true })
    }
  },

  input: function () { console.log("input") },
  transpond: function () { console.log("transpond") },
  collect: function () { console.log("collect") }
})

// 彈出動畫
function popp() {
  // plus 順時針旋轉
  var animationPlus = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })
  var animationcollect = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })
  var animationTranspond = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })
  var animationInput = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })

  animationPlus.rotateZ(180).step();
  animationcollect.translate(-100, -100).rotateZ(180).opacity(1).step();
  animationTranspond.translate(-140, 0).rotateZ(180).opacity(1).step();
  animationInput.translate(-100, 100).rotateZ(180).opacity(1).step();

  this.setData({
    animationPlus: animationPlus.export(),
    animationcollect: animationcollect.export(),
    animationTranspond: animationTranspond.export(),
    animationInput: animationInput.export(),
  })
}

// 收回動畫
function takeback() {
  // plus 逆時針旋轉回原位
  var animationPlus = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })
  var animationcollect = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })
  var animationTranspond = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })
  var animationInput = wx.createAnimation({ duration: 500, timingFunction: 'ease-out' })

  animationPlus.rotateZ(0).step();
  animationcollect.translate(0, 0).rotateZ(0).opacity(0).step();
  animationTranspond.translate(0, 0).rotateZ(0).opacity(0).step();
  animationInput.translate(0, 0).rotateZ(0).opacity(0).step();

  this.setData({
    animationPlus: animationPlus.export(),
    animationcollect: animationcollect.export(),
    animationTranspond: animationTranspond.export(),
    animationInput: animationInput.export(),
  })
}

要注意這裡 function 宣告在 Page 之後,卻能在 plus 中被呼叫,靠的是 JavaScript 的 function declaration 提升(hoisting)特性。若改成 var popp = function () {} 這種寫法就會出錯,因為變數宣告不會被提升。

二、index.wxml

<!--index.wxml-->
<image src="../../images/collect.png"
       animation="{{animationcollect}}"
       class="image-style"
       bindtap="collect"></image>

<image src="../../images/transpond.png"
       animation="{{animationTranspond}}"
       class="image-style"
       bindtap="transpond"></image>

<image src="../../images/input.png"
       animation="{{animationInput}}"
       class="image-style"
       bindtap="input"></image>

<image src="../../images/plus.png"
       animation="{{animationPlus}}"
       class="image-plus-style"
       bindtap="plus"></image>

三、index.wxss

/**index.wxss**/
.image-style {
  height: 150rpx;
  width: 150rpx;
  position: absolute;
  bottom: 250rpx;
  right: 100rpx;
  opacity: 0;
}

.image-plus-style {
  height: 150rpx;
  width: 150rpx;
  position: absolute;
  bottom: 250rpx;
  right: 100rpx;
  z-index: 100;
}

這裡有兩個關鍵設定:三個功能圖示預設 opacity: 0,所以未展開時是隱形的,動畫把它們的透明度推到 1 才會浮現;而「+」按鈕用 z-index: 100 壓在最上層,確保點擊永遠命中它而不是被散開的圖示蓋住。三個圖示都設成同一個 bottom/right 起始點,位移值才有共同的基準。

另外提醒一下,原始範例中 plus 與 takeback 的呼叫關係是反的(isPopping 為 true 時呼叫 popp),照抄時記得對照邏輯調整,或直接把它們改名為 expand/collapse 比較不容易搞混。

demo 程式碼下載。以上就是這次的實作分享,希望對你的小程序開發有幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved