美文网首页我的 ionicIonic之路ionic3+
ionic2实战-自定义modal过渡动画

ionic2实战-自定义modal过渡动画

作者: 昵称已被使用_ | 来源:发表于2017-07-09 21:49 被阅读1939次

需求

  • 要实现如下图的搜索界面效果该如何实现?
  • 有ionic开发经验的同学,看到这个界面很容易想到Ionic Modal,但是Modal默认过渡动画是从页面底部弹上来,而我们需要从页面右侧弹出页面,这时就需要自定义modal的过渡动画

实现效果图

  • 效果图代码已上传到github

上代码

  • 上面gif效果图提供了两个动画,我这里代码只附上动画2代码
  • 新建一个modal-transitions.ts文件用于定义modal过渡动画
import {Animation, PageTransition} from 'ionic-angular';

export class ModalFromRightEnter extends PageTransition {
  public init() {
    const ele = this.enteringView.pageRef().nativeElement;
    const backdrop = new Animation(this.plt, ele.querySelector('ion-backdrop'));
    backdrop.beforeStyles({'z-index': 0, 'opacity': 0.3, 'visibility': 'visible'});
    const wrapper = new Animation(this.plt, ele.querySelector('.modal-wrapper'));
    wrapper.fromTo('transform', 'translateX(100%)', 'translateX(20%)');
    const contentWrapper = new Animation(this.plt, ele.querySelector('ion-content.content'));
    contentWrapper.beforeStyles({'width': '80%'});
    this
      .element(this.enteringView.pageRef())
      .duration(300)
      .easing('cubic-bezier(.25, .1, .25, 1)')
      .add(backdrop)
      .add(wrapper)
      .add(contentWrapper);
  }
}

export class ModalFromRightLeave extends PageTransition {
  public init() {
    const ele = this.leavingView.pageRef().nativeElement;
    const backdrop = new Animation(this.plt, ele.querySelector('ion-backdrop'));
    backdrop.beforeStyles({'visibility': 'hidden'});
    const wrapper = new Animation(this.plt, ele.querySelector('.modal-wrapper'));
    wrapper.fromTo('transform', 'translateX(20%)', 'translateX(100%)');
    this
      .element(this.leavingView.pageRef())
      .duration(300)
      .easing('cubic-bezier(.25, .1, .25, 1)')
      .add(backdrop)
      .add(wrapper);
  }
}


export class ModalScaleEnter extends PageTransition {
  public init() {
    const ele = this.enteringView.pageRef().nativeElement;
    const wrapper = new Animation(this.plt, ele.querySelector('.modal-wrapper'));
    wrapper.fromTo('transform', 'scale(0)', 'scale(1)');

    this
      .element(this.enteringView.pageRef())
      .duration(400)
      .easing('cubic-bezier(.1, .7, .1, 1)')
      .add(wrapper);
  }
}

export class ModalScaleLeave extends PageTransition {
  public init() {
    const ele = this.leavingView.pageRef().nativeElement;
    const wrapper = new Animation(this.plt, ele.querySelector('.modal-wrapper'));
    wrapper.fromTo('transform', 'scale(1)', 'scale(0)');

    this
      .element(this.leavingView.pageRef())
      .duration(400)
      .easing('cubic-bezier(.1, .7, .1, 1)')
      .add(wrapper);
  }
}
  • app.module.ts文件中配置过渡动画
import {Config} from 'ionic-angular';
import {ModalFromRightEnter, ModalFromRightLeave} from "./modal-transitions";

export class AppModule {
  constructor(public config: Config) {
    this.setCustomTransitions();
  }

  private setCustomTransitions() {
    this.config.setTransition('modal-from-right-enter', ModalFromRightEnter);
    this.config.setTransition('modal-from-right-leave', ModalFromRightLeave);
  }
}

使用

  • 还不会使用modal去看api,使用我们自定义的动画如下图,给create方法传入第三个参数即可
 this.modalCtrl.create(ModalFromRightPage, {}, {
      enterAnimation: 'modal-from-right-enter',
      leaveAnimation: 'modal-from-right-leave'
    }).present();

最后

  • 请认真看modal-transitions.ts定义的动画代码,看懂后就可以定义更多更漂亮的动画;不止可以定义modal动画,还可以自定义page,toast,popover等其他组件动画
  • 在定义动画过程中可以学习源码是怎么定义的,如下gif带你找源码中定义的过渡动画
3.gif

相关文章

网友评论

  • shen1996:学习下 我之前的过渡都是用jq写的。。。。
  • 穎_ead5:Error: No component factory found for ModalFromRightPage. Did you add it to @NgModule.entryComponents?
    为什么我报这个错?
  • 鬼厉95:我发现了了一个小问题,把
    IonicModule.forRoot(MyApp, {
    mode: 'md',//苹果是'ios'
    backButtonText: '',
    tabsHideOnSubPages: 'true'
    }),
    mode的ios换成md,过渡动画出不来
    大唐帝国:发现一个问题,打包成web版本,在安卓浏览器中没效果
    鬼厉95:@小军617 动画这个要看哪些文档,我感觉很懵啊
    昵称已被使用_:@鬼厉95 已经修复了
    https://github.com/yanxiaojun617/ionic2_tabs/blob/master/src/app/modal-transitions.ts添加了12行和54行
  • ceido:军哥,ionic3能照搬吗?我试了一下,页面没变化,但输出modal已加载,大概是modal已经覆盖了页面,但是没显示出来
    昵称已被使用_:照搬,没问题
    ceido:算了,ionic3用菜单就可以实现了
  • Kathy丶Andy:还有如果想让像侧边栏那样 能滑动可以么
  • Kathy丶Andy:军哥 为啥动画按钮第二次点按钮需要点两次呢 还有如果不设置成ios 统一风 在安卓上不管用
  • f32180e135bc:在modal页请求回来的数据怎么传到原先的页面
    f32180e135bc:@小军617 成功拿到数据了,谢谢。还想问下选多个品牌的时候,要如何添加样式?以及展示全部的效果要怎么做?:joy: 刚接触ionic2,问题有点多,请见谅
    昵称已被使用_: this.viewCtrl.dismiss(data);//参数是要传回去的数据
    昵称已被使用_:let modal = this.modalCtrl.create(''')
    modal.present();
    modal.onDidDismiss(data => {
    if (data) {
    //数据在这里
    }
    });
  • 李大鱼_:为什么提示'modal-transitions.ts' is not a module.?
    李大鱼_:@小军617 恩已经成功了,谢谢
    昵称已被使用_:再认真阅读本文
  • 蛋蛋的蛋蛋凹凸曼:请问为什么会出现内存泄漏,闪退现象
  • 乜尔001:打包成ios的应用,会出现发热和耗电快的现象吗?
  • Jianshu9527:感谢分享
    昵称已被使用_:@一个大猩猩 :stuck_out_tongue_winking_eye:
  • 胖子饭店::+1: :+1: 最近正在为IOS上的动画切换烦恼呢,谢谢小军的这篇文章指导
    孔孔孔先生:@小军617 android下不好使
    昵称已被使用_:不用谢,我也是刚好有这需求

本文标题:ionic2实战-自定义modal过渡动画

本文链接:https://www.haomeiwen.com/subject/hlsehxtx.html