Ionic 2中选项卡之间转换时的动画

raj*_*raj 3 ionic-framework ionic2

我有一个带有基本标签的离子2应用程序.

<ion-tabs selectedIndex="1">
  <ion-tab [root]="tab1Root" tabTitle="hunts" tabIcon="book"></ion-tab>
  <ion-tab [root]="tab2Root" tabTitle="home" tabIcon="home"></ion-tab>
  <ion-tab [root]="tab3Root" tabTitle="hunting" tabIcon="locate"></ion-tab>
</ion-tabs>
Run Code Online (Sandbox Code Playgroud)

标签显示完美.但我想在标签之间转换时有一个滑动动画.也就是说,当我从中间选项卡按下最右边的选项卡时,中间选项卡应向左滑动,最右边的选项卡应从右侧滑入.这是一个基本的android动画.

我无法找到有关如何完成此任务的任何细节.这在Ionic(2.2.0)中仍然不可能吗?

Gui*_*ère 13

在开始之前

$ ionic cordova plugin add com.telerik.plugins.nativepagetransitions
$ npm install --save @ionic-native/native-page-transitions
Run Code Online (Sandbox Code Playgroud)

然后导入插件,在您src/app/app.module.ts通过以下方式

import { NativePageTransitions } from '@ionic-native/native-page-transitions';
Run Code Online (Sandbox Code Playgroud)

并且将其添加为供应商.

  providers: [
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    StatusBar,
    SplashScreen,
    NativePageTransitions // <-- add me here
  ]
Run Code Online (Sandbox Code Playgroud)

去你的 src/pages/tabs/tabs.ts

导入插件

import { NativePageTransitions, NativeTransitionOptions } from '@ionic-native/native-page-transitions';
Run Code Online (Sandbox Code Playgroud)

将插件添加到构造函数中

  constructor(public navCtrl: NavController,
              private nativePageTransitions: NativePageTransitions) {
  }
Run Code Online (Sandbox Code Playgroud)

添加这两个变量

loaded:   boolean = false;
tabIndex: number  = 0;
Run Code Online (Sandbox Code Playgroud)

然后添加这两个函数

  private getAnimationDirection(index:number):string {
    var currentIndex = this.tabIndex;

    this.tabIndex = index;

    switch (true){
      case (currentIndex < index):
        return('left');
      case (currentIndex > index):
        return('right');
    }
  }

  public transition(e:any):void {    
    let options: NativeTransitionOptions = {
     direction:this.getAnimationDirection(e.index),
     duration: 250,
     slowdownfactor: -1,
     slidePixels: 0,
     iosdelay: 20,
     androiddelay: 0,
     fixedPixelsTop: 0,
     fixedPixelsBottom: 48
    };

    if (!this.loaded) {
      this.loaded = true;
      return;
    }

    this.nativePageTransitions.slide(options);
  }
Run Code Online (Sandbox Code Playgroud)

然后转到src/pages/tabs/tabs.html 以下方式修改ion-tabs标签:

<ion-tabs (ionChange)="transition($event)">
Run Code Online (Sandbox Code Playgroud)

要么

<ion-tabs (ionSelect)="transition($event)">
Run Code Online (Sandbox Code Playgroud)

请享用.

PS:你可以在我的gist glemiere/58d1707ba82d40a55967601dbd0e72c9上找到完整的代码.

编辑 如果要修改动画,以下是您需要的所有信息. http://plugins.telerik.com/cordova/plugin/native-page-transitions


Mac*_*ski 8

我刚才想出了一个不同的解决方案...呵呵,这是一个有趣的想法,一个原始的想法,但是它在2分钟内给了您一半的过渡感觉

纯CSS:

  1. 全局应用程序的scss文件中,创建一个css选择器,该选择器将抓取加载到<ion-tabs>的路由器出口中的组件
  2. 在开始:P处添加一个CSS动画 -每次插入新组件时将运行该动画

app-e-tabs ion-router-outlet > .ion-page { 
  animation: fadeIn 0.3s 1 forwards;
}
@keyframes fadeIn {
  0% {
    opacity: 0;
    transform: translate3d(0, 5vh, 0);
  }
  100% {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}
Run Code Online (Sandbox Code Playgroud)

当然,可能有成千上万的潜在问题,而且它永远不会看起来像是正确的页面过渡..但是总比没有好:)

编辑:正如某些人指出的那样,您应该app-e-tabs从css的第一行中删除-这是我的应用程序中模块的名称-这就是为什么它会出现在我的代码中的原因。

  • 很棒的解决方案!我必须删除“app-e-tabs”才能使其工作。这个解决方案值得更多的赞誉。:-) (2认同)
  • 删除“app-e-tabs”并将上面的代码粘贴到“global.scss”中效果非常好! (2认同)