DIV CSS 佈局教程網

JS 打造跨平台手勢動畫:Android 與 iOS 一致的翻頁效果
編輯:關於JavaScript     

先看效果:

上圖是 iOS 下的呈現,Android 端完全一致。透過 do_GestureView 與 do_Animation 兩個元件,DeviceOne 能相對容易地做出複雜的跨平台原生動畫。這個範例以手勢控制圖片上下滑動,達到開闔式的翻頁效果,並可搭配音效。

主要程式碼:

 // index.ui.js
var do_Animator1 = mm("do_Animator");
do_Animator1.append(500, { y: -1334, curve: "Linear" });

var do_Animator2 = mm("do_Animator");
do_Animator2.append(500, { y: 0, curve: "Linear" });

do_Page.on("NextPagemoveing", function () {
  currentView = json_define[currentIndex].view;
  currentIndex++;
  if (currentIndex >= json_define.length) currentIndex = 0;
  nextView = json_define[currentIndex].view;
  for (var i = 0; i < json_define.length; ++i) {
    if (json_define[i].view != currentView && json_define[i].view != nextView) {
      json_define[i].view.visible = false;
    } else {
      json_define[i].view.visible = true;
    }
  }
  currentView.fire("closingBottom", "NextPagemove");
});

do_Page.on("NextPagemove", function () {
  nextView.fire("initMoving", "NextPagemoved");
});

do_Page.on("NextPagemoved", function () {
  currentView.y = 0;
  nextView.y = 1332;
  do_ALayout_main.y = 0;
  do_ALayout_main.redraw();
  do_ALayout_main.animate(do_Animator1, function () {
    nextView.fire("opening");
  });
});

do_Page.on("PrePagemoveing", function () {
  currentView = json_define[currentIndex].view;
  currentIndex--;
  if (currentIndex < 0) currentIndex = json_define.length - 1;
  nextView = json_define[currentIndex].view;
  currentView.fire("closingHead", "PrePagemove");
});

do_Page.on("PrePagemove", function () {
  nextView.fire("initMoving", "PrePagemoved");
});

do_Page.on("PrePagemoved", function () {
  for (var i = 0; i < json_define.length; ++i) {
    if (json_define[i].view != currentView && json_define[i].view != nextView) {
      json_define[i].view.visible = false;
    } else {
      json_define[i].view.visible = true;
    }
  }
  currentView.y = 1332;
  nextView.y = 0;
  do_ALayout_main.y = -1334;
  do_ALayout_main.redraw();
  do_ALayout_main.animate(do_Animator2, function () {
    nextView.fire("opening");
  });
});

整份邏輯可以拆成三層來讀。最外層是 do_Page 的六個翻頁事件,分成「下一頁」與「上一頁」兩組,每組各有 moveing/move/moved 三個階段。中間層負責決定 currentView 與 nextView 是誰,並把其餘頁面設為不可見,避免不必要的繪製。最內層才是動畫本身:兩個 do_Animator 分別負責 y 軸從 0 到 -1334 與反向的位移,animate 的第二個參數是完成後的回呼,用來觸發下一頁的 opening 事件。

這種「狀態機 + 事件廣播」的組織方式很值得借鏡:把「哪個畫面該出現」與「畫面怎麼動」拆開,各自處理自己的事,後續要加音效、加轉場或換成左右翻頁,只需替換對應階段的事件處理,不必動到整體結構。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved