美文网首页
VC转场动画 UIViewControllerTransitio

VC转场动画 UIViewControllerTransitio

作者: woo_5857 | 来源:发表于2022-08-11 14:53 被阅读0次

    //

    //  BNBaseAnimationVC.h

    //  LFZCustomView_Example

    //

    //  Created by boniu on 2022/8/12.

    //  Copyright © 2022 laofuzi. All rights reserved.

    //

    #import <UIKit/UIKit.h>

    @interface BNBaseAnimationVC : UIViewController<UIViewControllerTransitioningDelegate, UINavigationControllerDelegate>

    @property (nonatomic, strong) UIView *curAnimationView;

    - (instancetype)initWith:(BOOL)isAni isPush:(BOOL)isPush type:(NSInteger)type;

    @end

    @interface BNBaseAnPresent : NSObject<UIViewControllerAnimatedTransitioning>

    @property (nonatomic, assign)BOOL needTM;

    @end

    @interface BNBaseAnDismiss : NSObject<UIViewControllerAnimatedTransitioning>

    @end

    //

    //  BNBaseAnimationVC.m

    //  LFZCustomView_Example

    //

    //  Created by boniu on 2022/8/12.

    //  Copyright © 2022 laofuzi. All rights reserved.

    //

    #import "BNBaseAnimationVC.h"

    @interface BNBaseAnimationVC ()

    @end

    @implementation BNBaseAnimationVC

    - (instancetype)initWith:(BOOL)isAni isPush:(BOOL)isPush type:(NSInteger)type  {

        if (self = [super init]) {

            if (isAni) {

                if (isPush) {

                    self.modalPresentationStyle = UIModalPresentationCustom;    // 自定义转场模式

                } else {

                    self.transitioningDelegate = self;  // 设置自己为转场代理

                    self.modalPresentationStyle = UIModalPresentationCustom;    // 自定义转场模式

                }

            }

        }

        return self;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        [self.view addSubview:self.curAnimationView];

        self.curAnimationView.backgroundColor = UIColor.redColor;

    }

    - (void)viewWillAppear:(BOOL)animated {

        [super viewWillAppear:animated];

        self.navigationController.delegate = self;

    }

    - (UIView *)curAnimationView {

        if (!_curAnimationView) {

            _curAnimationView = [[UIView alloc] init];

        }

        return _curAnimationView;

    }

    #pragma mark - UINavigationControllerDelegate

    - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {

        if (toVC == self) {

            BNBaseAnPresent * pre = [[BNBaseAnPresent alloc]init];

            return pre;

        } else {

    //        BNBaseAnDismiss * pre = [[BNBaseAnDismiss alloc]init];

    //        return pre;

            return nil;

        }

    }

    #pragma mark - UIViewControllerTransitioningDelegate

    //入场

    - (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {

        BNBaseAnPresent * pre = [[BNBaseAnPresent alloc]init];

        return pre;

    }

    //出厂

    - (id<UIViewControllerAnimatedTransitioning>) animationControllerForDismissedController:(UIViewController *)dismissed {

        return [[BNBaseAnDismiss alloc] init];

    }

    @end

    @implementation BNBaseAnPresent

    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {

        return 0.3;

    }

    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

    //    BNBaseAnimationVC *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

        BNBaseAnimationVC *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

        toVC.curAnimationView.alpha = 0;

        UIView *containerView = [transitionContext containerView];

        [containerView addSubview:toVC.view];

        NSTimeInterval duration = [self transitionDuration:transitionContext];

        toVC.curAnimationView.frame = CGRectMake(toVC.view.frame.size.width/2, toVC.view.frame.size.width/2, 0, 0);

        [UIView animateWithDuration:duration

                         animations:^{

            toVC.curAnimationView.alpha = 1;

            toVC.curAnimationView.frame = CGRectMake(0, 0, toVC.view.frame.size.width, toVC.view.frame.size.height);

    //        toVC.curAnimationView.transform = CGAffineTransformIdentity;

        }

                         completion:^(BOOL finished) {

            [transitionContext completeTransition:YES];

        }];

    }

    @end

    @implementation BNBaseAnDismiss

    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {

        return 0.15;

    }

    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

        BNBaseAnimationVC *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    //    BNBaseAnimationVC *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

        NSTimeInterval duration = [self transitionDuration:transitionContext];

        [UIView animateWithDuration:duration

                         animations:^{

            fromVC.curAnimationView.alpha = 0;

            fromVC.curAnimationView.frame = CGRectMake(fromVC.view.frame.size.width/2, fromVC.view.frame.size.width/2, 0, 0);

        }

                         completion:^(BOOL finished) {

            [transitionContext completeTransition:YES];

        }];

    }

    @end

    相关文章

      网友评论

          本文标题:VC转场动画 UIViewControllerTransitio

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