DIV CSS 佈局教程網

解決 Ionic 頁面切換卡頓:改用 Native Transitions
編輯:關於JavaScript     

用 Ionic 開發 App 時,頁面切換動畫常常出現卡頓,不夠順滑。為了保住使用體驗,很多人乾脆用 $ionicConfigProvider.views.transition('no') 把動畫關掉,但這只是退而求其次。Cordova 提供了 native transitions,能讓頁面切換接近原生的流暢度。以下說明主要步驟。

安裝與引入

1. 執行 npm install ionic-native-transitions --save 下載檔案,並放入 www/lib 目錄。

2. 在 index.html 中加入:

<script src="lib/ionic-native-transitions/dist/ionic-native-transitions.min.js"></script>

3. 執行 cordova plugin add cordova-plugin-NativePageTransitions 安裝 Cordova 插件。

在 app.js 中設定

4. 引入 'ionic-native-transitions',同時停用原本的 transition 設定:

 $ionicNativeTransitionsProvider.setDefaultOptions({ duration: 400, // in milliseconds (ms), default 400, slowdownfactor: 4, // overlap views (higher number is more) or no overlap (1), default 4 iosdelay: -1, // ms to wait for the iOS webview to update before animation kicks in, default -1 androiddelay: -1, // same as above but for Android, default -1 winphonedelay: -1, // same as above but for Windows Phone, default -1, fixedPixelsTop: 0, // the number of pixels of your fixed header, default 0 (iOS and Android) fixedPixelsBottom: 0, // the number of pixels of your fixed footer (f.i. a tab bar), default 0 (iOS and Android) triggerTransitionEvent: '$ionicView.afterEnter', // internal ionic-native-transitions option backInOppositeDirection: false // Takes over default back transition and state back transition to use the opposite direction transition to go back }); 

設定完成後打包,切頁效果會比 Ionic 自帶的動畫順暢不少。

一個容易忽略的細節:返回方向

注意:頁面切換方向上,後退用的 <ion-nav-back-button> 預設是由左往右,其他情況則是由右往左。如果你沒有使用 <ion-nav-back-button> 這個標籤,而是在 <ion-nav-bar> 裡自訂後退按鈕,方向就會反過來變成由右往左。要讓它維持由左往右,應該呼叫 $rootScope.$ionicGoBack(),而不是 $ionicHistory.goBack()。

更多說明可參考專案頁面:https://github.com/shprink/ionic-native-transitions

以上就是 Ionic 頁面切換卡頓的解法,希望對你有所幫助。

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