美文网首页
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

相关文章

  • iOS转场动画

    转场动画,就是Vc切换过程中的过渡动画。 官方支持以下几种方式的自定义转场: UINavigationContro...

  • iOS 转场动画

    转场动画,就是Vc切换过程中的过渡动画。官方支持以下几种方式的自定义转场:1、我们最常见的在 UINavigati...

  • iOS-转场动画之神奇移动效果

    今天来做一下神奇移动效果,利用了present的转场动画: 我总结了一下使用转场动画的过程:(假设是从VC1 pr...

  • iOS添加自定义转场动画和交互动画(一)

    准备写两篇,第一篇介绍下转场动画,第二篇介绍下我封装的一个转场动画的库,可以很简便的给VC之间的转变加上自定义动画...

  • iOS-自定义控制器转场动画(present/dismiss)

    一、转场动画类型 iOS控制器转场动画类型可以分为非交互式转场动画和交互式转场动画。 二、转场动画分析 2.1、转...

  • 聊聊Android的转场动画

    Android的转场动画包括:场景动画,页面的转场动画,页面元素间共享动画。 Android转场动画[https:...

  • iOS - 转场动画

    参考文章:iOS 转场动画一张图看懂 iOS 转场动画iOS自定义转场动画 iOS 转场动画探究(一)

  • iOS 自定义VC切换动画

    在有些情况下,系统自带的导航的push动画无法满足我们APP的设计需求,我们需要自定义VC的转场动画,下面是我参照...

  • iOS开发——登录页面动画、转场动画

    iOS开发——登录页面动画、转场动画 iOS开发——登录页面动画、转场动画

  • UIView动画

    1.转场动画Transitions – UIView Transition (视图转场动画)

网友评论

      本文标题:VC转场动画 UIViewControllerTransitio

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