iOS自定義轉(zhuǎn)場(chǎng)動(dòng)畫(3)——自定義模態(tài)跳轉(zhuǎn)之Present

Modal

modal轉(zhuǎn)場(chǎng)方式即使用 presentViewController() 方法推出的方式,默認(rèn)情況下,第二個(gè)視圖從屏幕下方彈出。下面就來(lái)介紹下 modal 方式轉(zhuǎn)場(chǎng)動(dòng)畫的自定義。

present

還是先來(lái)看一下完成的效果


準(zhǔn)備

  1. 創(chuàng)建一個(gè)新的工程,刪掉Main,在AppDelegate中創(chuàng)建自定義UIWindow,設(shè)置rootVC為ViewController。
  2. 在ViewController中創(chuàng)建一個(gè)全屏的ImageView,并且指定一張圖片;新建一個(gè)按鈕用于present。
  3. 新建一個(gè)SecondViewController。同樣創(chuàng)建一個(gè)全屏的ImageView,指定一張和ViewController不同的圖片,新建一個(gè)同于dismiss的按鈕

開始

  1. 創(chuàng)建一個(gè)文件繼承自 NSObject,取名為PresentTransition并在.h中遵守 UIViewControllerAnimatedTransitioning協(xié)議。
  2. 實(shí)現(xiàn)協(xié)議的兩個(gè)方法,并在其中編寫 Push 的動(dòng)畫。 具體的動(dòng)畫實(shí)現(xiàn)過(guò)程都在代碼的注釋里:
// 返回動(dòng)畫的時(shí)間
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return 0.8;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    ViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    SecondViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView * container = [transitionContext containerView];
    
    [container addSubview:toVC.view];
    [container bringSubviewToFront:fromVC.view];
    
    // 改變m34
    CATransform3D transfrom = CATransform3DIdentity;
    transfrom.m34 = -0.002;
    container.layer.sublayerTransform = transfrom;
    
    // 設(shè)置archPoint和position
    CGRect initalFrame = [transitionContext initialFrameForViewController:fromVC];
    toVC.view.frame = initalFrame;
    fromVC.view.frame = initalFrame;
    fromVC.view.layer.anchorPoint = CGPointMake(0, 0.5);
    fromVC.view.layer.position = CGPointMake(0, initalFrame.size.height / 2.0);
    
    // 添加陰影效果
    CAGradientLayer * shadowLayer = [[CAGradientLayer alloc] init];
    shadowLayer.colors =@[
                         [UIColor colorWithWhite:0 alpha:1],
                         [UIColor colorWithWhite:0 alpha:0.5],
                         [UIColor colorWithWhite:1 alpha:0.5]
                         ];
    shadowLayer.startPoint = CGPointMake(0, 0.5);
    shadowLayer.endPoint = CGPointMake(1, 0.5);
    shadowLayer.frame = initalFrame;
    
    UIView * shadow = [[UIView alloc] initWithFrame:initalFrame];
    shadow.backgroundColor = [UIColor clearColor];
    [shadow.layer addSublayer:shadowLayer];
    [fromVC.view addSubview:shadow];
    shadow.alpha = 0;
    
    // 動(dòng)畫
    [UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:2 animations:^{
        fromVC.view.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
        shadow.alpha = 1.0;
    } completion:^(BOOL finished) {
        fromVC.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
        fromVC.view.layer.position = CGPointMake(CGRectGetMidX(initalFrame), CGRectGetMidY(initalFrame));
        fromVC.view.layer.transform = CATransform3DIdentity;
        [shadow removeFromSuperview];
        
        [transitionContext completeTransition:YES];
    }];
}

使用動(dòng)畫

  1. 讓 FirstViewController 遵守 UIViewControllerTransitioningDelegate 協(xié)議,并將 self.transitioningDelegate 設(shè)置為 self。
self.transitioningDelegate = self;
  1. 實(shí)現(xiàn) UIViewControllerTransitioningDelegate 協(xié)議的兩個(gè)方法,用來(lái)指定動(dòng)畫類。
// present動(dòng)畫
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [[PresentTransition alloc] init];
}
  1. 在present按鈕的點(diǎn)擊方法中,必須把SecondViewController的transitioningDelegate也設(shè)置為self,讓ViewController管理SecondViewController的動(dòng)畫
-(void)presentClick{
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    secondVC.transitioningDelegate = self; // 必須second同樣設(shè)置delegate才有動(dòng)畫
    [self presentViewController:secondVC animated:YES completion:^{
    }];
}

自定義Present動(dòng)畫完成

點(diǎn)擊此處下載源碼

最后編輯于
?著作權(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)容