溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

UIKit框架(10)自定義modal過渡效果

發(fā)布時間:2020-09-02 10:04:36 來源:網(wǎng)絡(luò) 閱讀:644 作者:ymanmeng123 欄目:移動開發(fā)

上一篇文章介紹了如何進(jìn)行modal方式的頁面切換

自定義的目的控制器在modal切換時完全覆蓋源控制器,本篇文章介紹如何實(shí)現(xiàn)一個自定義的過渡效果


實(shí)現(xiàn)的效果描述:

    目的控制器:占據(jù)屏幕的二分之一大小,且居中,并在源控制器上覆蓋著一個陰影效果,點(diǎn)擊陰影時目的控制器返回


  • UIPresentationController

用于描述目的控制器通過modal方式切換的過渡效果

實(shí)現(xiàn)其子類,可以自定義出特殊的效果


實(shí)現(xiàn)步驟:

  1. 定義過渡效果:實(shí)現(xiàn)UIPresentationController子類

  2. 目的控制器,遵循過渡協(xié)議,設(shè)置過渡控制器對象

  3. 源控制器進(jìn)行modal切換


UIPresentationController的屬性:

@property(nonatomic, retain, readonly) UIViewController *presentedViewController   //目的控制器
@property(nonatomic, retain, readonly) UIViewController*presentingViewController   //源控制器
- (UIView *)presentedView  //目的view
     @property(nonatomic, readonly) UIView *containerView  //源view


  • UIPresentationController的子類應(yīng)重寫的方法

1)init方法,可以創(chuàng)建其他輔助過渡效果的view

- (instancetype)initWithPresentedViewController:(UIViewController*)presentedViewController presentingViewController:(UIViewController*)presentingViewController

    如:

- (instancetype)initWithPresentedViewController:(UIViewController*)presentedViewController presentingViewController:(UIViewController*)presentingViewController {    
    if ( self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController] ) {
        _shadowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //陰影按鈕的初始狀態(tài)是隱藏的
        _shadowBtn.backgroundColor = [UIColor grayColor];
        _shadowBtn.alpha = 0.f;
    }
}


2)重寫presentationTransitionWillbegin方法,定義實(shí)現(xiàn)過渡效果

- (void)presentationTransitionWillBegin

    如:

- (void)presentationTransitionWillBegin
{
    [self.containerView addSubview:_shadowBtn];
    [self.containerView addSubview:self.presentedView];
    _shadowBtn.frame = self.containerView.bounds;
    id <UIViewControllerTransitionCoordinator> coordinate = self.presetingViewController.transitionCoordinator;
    [coordinate animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
        _shadowBtn.alpha = 0.5;
    } completion:nil];
}

    使用到UIViewController的transitionCoordinator,表示過渡效果的協(xié)助器

     協(xié)助器的animateAlongsideTransition方法定義過渡期間的動畫效果


3)重寫presentationTransitionDidEnd方法,定義過渡效果后的清理工作。

    特別是過渡未完成時的清理動作

- (void)presentationTransitionDidEnd:(BOOL)completed
- (void)presentationTransitionDidEnd:(BOOL)completed
{
    if ( !completed ) {
        [_shadowBtn removeFromSuperview];
    }
}


4)重寫frameOfPresentedViewInContainerView,設(shè)置被顯示視圖的frame

- (CGRect)frameOfPresentedViewInContainerView
- (CGRect)frameOfPresentedViewInContainerView
{
    CGFloat x, y, w, h;
    w = self.containerView.frame.size.width/2;
    h = self.containerView.frame.size.height/2;
    x = self.containerView.frame.size.width/4;
    y = self.containerView.frame.size.height/4;
    return CGRectMake(x, y, w, h);
}


5)重寫dismissTransitionWillBegin方法,設(shè)置返回的過渡效果

- (void)dismissalTransitionWillBegin
- (void)dismissalTransitionWillBegin
{
    id<UIViewControllerTransitionCoordinator> coordinator = self.presetingViewController.transitionCoordinator;
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonull context) {
        _shadowBtn.alpha = 0.01;
    } completion:nil];
}

6)重寫dismissalTransitionDidEnd方法,執(zhí)行清理動作

- (void)dismissalTransitionDidEnd:(BOOL)completed
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
    if ( completed ) {
        [_shadowView removeFromSuperview];
    }
}

     


  • 目的控制器設(shè)置過渡效果

目的控制器遵循代理協(xié)議

@interface AMDestViewController () <UIViewControllerTransitioningDelegate>

設(shè)置代理

self.transitioningDelegate = self;

實(shí)現(xiàn)代理方法

- (UIPresetationController *) presentationControllerForPresentedViewController:(UIViewController*) presented presentingViewController:(UIViewController*) presenting sourceViewController:(UIViewController*) source
{
    return [[AMPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}


  • 源控制器進(jìn)行modal切換

iOS8.0開始支持這種自定義的過渡效果,主要要設(shè)置目的控制器的modalPresentationStyle為自定義。

這種切換方式,在iPhone和iPad上都是可用的。

AMDestViewController * vc = [[AMDestViewController alloct] init];
vc.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:vc animated:YES completion:nil];




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI