ionic2/3實(shí)戰(zhàn)-自定義modal過渡動(dòng)畫

需求

  • 要實(shí)現(xiàn)如下圖的搜索界面效果該如何實(shí)現(xiàn)?
  • 有ionic開發(fā)經(jīng)驗(yàn)的同學(xué),看到這個(gè)界面很容易想到Ionic Modal,但是Modal默認(rèn)過渡動(dòng)畫是從頁面底部彈上來,而我們需要從頁面右側(cè)彈出頁面,這時(shí)就需要自定義modal的過渡動(dòng)畫

實(shí)現(xiàn)效果圖

  • 效果圖代碼已上傳到github

上代碼

  • 上面gif效果圖提供了兩個(gè)動(dòng)畫,我這里代碼只附上動(dòng)畫2代碼
  • 新建一個(gè)modal-transitions.ts文件用于定義modal過渡動(dòng)畫
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文件中配置過渡動(dòng)畫
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);
  }
}

使用

  • 還不會(huì)使用modal去看api,使用我們自定義的動(dòng)畫如下圖,給create方法傳入第三個(gè)參數(shù)即可
 this.modalCtrl.create(ModalFromRightPage, {}, {
      enterAnimation: 'modal-from-right-enter',
      leaveAnimation: 'modal-from-right-leave'
    }).present();

最后

  • 請(qǐng)認(rèn)真看modal-transitions.ts定義的動(dòng)畫代碼,看懂后就可以定義更多更漂亮的動(dòng)畫;不止可以定義modal動(dòng)畫,還可以自定義page,toast,popover等其他組件動(dòng)畫
  • 在定義動(dòng)畫過程中可以學(xué)習(xí)源碼是怎么定義的,如下gif帶你找源碼中定義的過渡動(dòng)畫
3.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容