VC转场动画 UIViewControllerTransitioningDelegate, UINavigationControllerDelegate

//

//  BNBaseAnimationVC.h

//  LFZCustomView_Example

//

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

//  Copyright © 2022 laofuzi. All rights reserved.

//

#import

@interface BNBaseAnimationVC : UIViewController

@property (nonatomic, strong) UIView *curAnimationView;

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

@end

@interface BNBaseAnPresent : NSObject


@property (nonatomic, assign)BOOL needTM;

@end

@interface BNBaseAnDismiss : NSObject

@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)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 )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {

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

    return pre;

}

//出厂

- (id) animationControllerForDismissedController:(UIViewController *)dismissed {

    return [[BNBaseAnDismiss alloc] init];

}

@end

@implementation BNBaseAnPresent

- (NSTimeInterval)transitionDuration:(id)transitionContext {

    return 0.3;

}

- (void)animateTransition:(id)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)transitionContext {

    return 0.15;

}

- (void)animateTransition:(id)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转场动画 UIViewControllerTransitioningDelegate, UINavigationControllerDelegate)