1. 程式人生 > >ionic頁面切換卡頓解決方法

ionic頁面切換卡頓解決方法

使用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>

2、cordova plugin add cordvoa-plugin-NativePageTransitions安裝該外掛

3、在app.js中引入'ionic-native-transitions'配置如下資訊並禁用$ionicConfigProvider.views.transition('no');

$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
});


這樣在打包成的app裡,切面切換的效果會比ionic自帶的要流暢不少;

注意:頁面切換的方向,後退<ion-nav-back-button>預設是左往右,其他則是右往左,有時候你可能並不用<ion-nav-back-button>這個標籤,而是使用<ion-nav-bar>該標籤,後退加自定義的東西,這時後退是按右往左,那怎樣左往右了,用$rootScope.$ionicGoBack();就可以,而不要使用$ionicHistory.goBack();之後的後退方法。

更多的可以看下這個地址

https://github.com/shprink/ionic-native-transitions