1.小记

  • 关于自定义转场动画,只要你理清他的”套路”,你就可以随心所欲地自定义了.
  • 大体思路就是:遵守对应的代理协议,然后设置对应的代理,实现代理方法,这个代理方法要返回的值就是你要实现的动画.(如果返回nil,就是默认效果)
  • 以UITabBarController为例的简单转场动画demo地址  gitHub地址

2.基本介绍

在此介绍一下基本知识:

1.在哪里写我们自定义的动画.

苹果给我们提供了UIViewControllerAnimatedTransitioning协议,这个协议提供了我们需要的接口,遵守这个协议的对象实现动画基本内容. 
让我们跳转进去看看都有什么:

@protocol UIViewControllerAnimatedTransitioning <NSObject>// This is used for percent driven interactive transitions, as well as for
// container controllers that have companion animations that might need to
// synchronize with the main animation.
// 这个接口返回的值为动画时长- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
// 这个接口返回的值为具体动画内容,也就是说,自定义的动画操作都通过这个接口来实现
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;@optional/// A conforming object implements this method if the transition it creates can
/// be interrupted. For example, it could return an instance of a
/// UIViewPropertyAnimator. It is expected that this method will return the same
/// instance for the life of a transition.
- (id <UIViewImplicitlyAnimating>) interruptibleAnimatorForTransition:(id <UIViewControllerContextTransitioning>)transitionContext NS_AVAILABLE_IOS(10_0);// This is a convenience and if implemented will be invoked by the system when the transition context's completeTransition: method is invoked.
- (void)animationEnded:(BOOL) transitionCompleted;@end
1.通过注释的解释,我们能够知道,遵守UIViewControllerAnimatedTransitioning协议的对象就可以实现我们自定义的动画.
2.通常我们会自定义NSObject的子类,遵守UIViewControllerAnimatedTransitioning协议,然后实现协议方法来自定义转场动画.
3.这个子类的对象就是我们的"自定义动画".如果把自定义转场动画比作为做菜的话,那么现在我们准备的就是食材.
  • 在这里要对一些概念进行下解释,避免在自定义动画时蒙圈
1.From和To
在自定义转场动画的代码中,经常会出现fromViewController和toViewController。如果错误的理解它们的含义会导致动画逻辑完全错误。
fromViewController表示当前视图容器,toViewController表示要跳转到的视图容器。如果是从A视图控制器present到B,则A是from,B是to。从B视图控制器dismiss到A时,B变成了from,A是to。
2.Presented和Presenting
这也是一组相对的概念,它容易与fromView和toView混淆。简单来说,它不受present或dismiss的影响,如果是从A视图控制器present到B,那么A总是B的presentingViewController, B总是A的presentedViewController。

2.在哪里用我们自定义的动画.

这里要介绍三个协议: 注意每个协议方法的返回值,都是遵守UIViewControllerAnimatedTransitioning的对象

1.协议一: UIViewControllerTransitioningDelegate
// 实现present/dismiss动画的接口.
// 令我们需要自定义动画的控制器遵守UIViewControllerTransitioningDelegate协议,并设置代理,实现协议方法,返回遵守UIViewControllerAnimatedTransitioning协议的类对象即可
// 在这里需要清楚一点,假设由控制器A present 到B, A遵守UIViewControllerTransitioningDelegate协议,则设置B.transitioningDelegate = A,并设置B.modalPresentationStyle = UIModalPresentationCustom(或UIModalPresentationFullScreen);
// 一定要设置modalPresentationStyle,不然还是默认的转场动画.// present动画
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;// dismiss动画
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
# modalPresentationStyl// 这是一个枚举类型,表示present时动画的类型。// 其中可以自定义动画效果的只有两种:FullScreen和Custom,两者的区别在于FullScreen会移除fromView,而Custom不会。
2.协议二:UINavigationControllerDelegate
// 实现push/pop动画的接口
// 这里同样是要遵守协议,设置代理,实现协议方法.
// 注意这里设置的是navigationController.delegate, self.navigationController.delegate = self.
// 我在其他的博客中看到: (注意: 这里的 self.navigationController.delegate = self 最好写在当前控制器的viewDidAppear方法中, 不然会导致在此push时无动画效果),为什么会失效我还不清楚,希望读者能够找到并分享一下~
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationControlleranimationControllerForOperation:(UINavigationControllerOperation)operationfromViewController:(UIViewController *)fromVCtoViewController:(UIViewController *)toVC
3.协议三:UITabBarControllerDelegate
// 实现tabBarController切换子控制器的动画
// 还是老套路,遵守协议,设置代理,实现协议方法
// 只是这里要设置tabBarController的代理,我的做法就是在UITabBarController的viewDidLoad方法里设置代理: self.delegate = self;
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarControlleranimationControllerForTransitionFromViewController:(UIViewController *)fromVCtoViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);
  • 小结: 从上面三个协议的返回值能够看出,返回的东西就是我们2.1自定义遵守UIViewControllerAnimatedTransitioning协议的类的对象.

3.转场动画的思路(纯个人理解,起个抛砖引玉作用~)

  • 步骤一: 明确做哪种转场动画(做哪种菜,是鲁菜,还是川菜?)
自定义present/dismiss动画要遵守UIViewControllerTransitioningDelegate协议
自定义push/pop动画要遵守UINavigationControllerDelegate协议
自定义tabbarController转场动画要遵守UITabBarControllerDelegate协议

以demo为例:

// DMMainViewController.m文件
@interface DMMainViewController ()<UITabBarControllerDelegate>// 遵守协议
@end
@implementation DMMainViewController
- (void)viewDidLoad {[super viewDidLoad];self.delegate = self;// 设置代理UIViewController *vc = [[UIViewController alloc] init];vc.view.backgroundColor = [UIColor whiteColor];[self setChildchildViewController:vc index:0 title:@"我是A"];[self setChildchildViewController:[[UITableViewController alloc] init] index:1 title:@"我是B"];[self setChildchildViewController:[[UIViewController alloc] init] index:2 title:@"我是C"];
}
// 动画 实现协议方法
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{return [[AnimationManager alloc] init];
}

这里其实就是遵守协议,设置代理,实现协议方法.

  • 步骤二: 确定做哪样转场动画(如果选择了鲁菜,是德州扒鸡,还是红烧大虾?如果选择了川菜,是四川火锅,还是水煮鱼?)
// AnimationManager.h文件
// 自定义NSObject的子类,遵守UIViewControllerAnimatedTransitioning协议
@interface AnimationManager : NSObject<UIViewControllerAnimatedTransitioning>@property (nonatomic, assign) KAnimationType type;
- (instancetype)initWithType:(KAnimationType)type;@end
// AnimationManager.m文件
#import "AnimationManager.h"
#import "DMNavigationViewController.h"@interface AnimationManager ()
@end@implementation AnimationManager// 这个是动画时长
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {return 0.5;
}// 具体动画,在这里可以根据你的想象去实现你要的动画效果了
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {// 获取fromVc和toVcDMNavigationViewController *fromVc = (DMNavigationViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];DMNavigationViewController *toVc = (DMNavigationViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];UIView *fromV = fromVc.view;UIView *toV = toVc.view;// 转场环境UIView *containView = [transitionContext containerView];containView.backgroundColor = [UIColor whiteColor];// 判断滑动方向if (toVc.index > fromVc.index) {toV.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, containView.frame.size.width, containView.frame.size.height);[containView addSubview:toV];// 动画[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{fromV.transform = CGAffineTransformTranslate(fromV.transform, -[UIScreen mainScreen].bounds.size.width,0);// containView.frame.size.heighttoV.transform = CGAffineTransformTranslate(toV.transform, -[UIScreen mainScreen].bounds.size.width, 0);} completion:^(BOOL finished) {[transitionContext completeTransition:YES];}];}else if (toVc.index < fromVc.index) {toV.frame = CGRectMake(- [UIScreen mainScreen].bounds.size.width, 0, containView.frame.size.width, containView.frame.size.height);[containView addSubview:toV];[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{fromV.transform = CGAffineTransformTranslate(fromV.transform, [UIScreen mainScreen].bounds.size.width,0);toV.transform = CGAffineTransformTranslate(toV.transform, [UIScreen mainScreen].bounds.size.width, 0);} completion:^(BOOL finished) {[fromV removeFromSuperview];[transitionContext completeTransition:YES];}];}
}
@end# 这里面就涉及到前面讲的 1.From和To的关系,2.Presented和Presenting的关系.在2.1的底部有介绍

小结:

所谓的自定义转场动画,就是把系统默认的换成我们自己写的而已,关键就是在这些协议里.理清控制器与协议的关系.

简单的画了一个结构图

附: 以UITabBarController为例的简单转场动画demo地址  gitHub地址

参考文章:iOS自定义转场动画, iOS中应该知道的自定义各种Controller的转场过渡动画

iOS开发 tabbar自定义转场动画相关推荐

  1. iOS开发之核心转场动画

    iOS开发UI篇-核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一 ...

  2. iOS 自定义转场动画浅谈

    代码地址如下: http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差 ...

  3. iOS 关于自定义转场动画,以UITabBarController为例

    1.小记 关于自定义转场动画,只要你理清他的"套路",你就可以随心所欲地自定义了. 大体思路就是:遵守对应的代理协议,然后设置对应的代理,实现代理方法,这个代理方法要返回的值就是你 ...

  4. iOS 自定义转场动画, nav的push/pop自定义动画

    本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动画.网易音乐启动屏转场动画.开关门动画.全屏侧滑返回效果 的代码可以到Github WSLTransferA ...

  5. iOS自定义转场动画实战讲解

    转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completio ...

  6. iOS 自定义转场动画篇

    前言: 自定义转场动画其实并不难, 关键在于能够明白思路, 也就是操作步骤. 本篇博客主要以present转场动画为例, 进行分析, 操作, 如有错误欢迎简信与我交流. 不进行修改的话, presen ...

  7. iOS 自定义转场动画实现小红书的push效果思路以及下雪碎屏等动画的实现

    感觉好久没写会动的Demo了,前几天写了很久的Block源码分析,分析了几天整个人都不好了,都不知道block是什么了......,有需要的同学可以去看看,简直不要太简单Block是什么鬼毕竟也是做电 ...

  8. 【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】

    iOS默认的push动画是把即将展示的控制器从右边推过来.有时我们想实现类似PPT中的一些动画,这时候就需要自定义转场动画了.如下图我们想实现一个淡出并且放大的过场动画,在退出时是一个淡出缩小的动画. ...

  9. iOS 开发之玩转专场动画

    有空了再造个轮子,先放个链接 WWDC 2013 Session笔记 - iOS7中的ViewController切换 几句代码快速集成自定义转场效果+ 全手势驱动 TransitionAnimati ...

最新文章

  1. HCNE题库附件、可下载
  2. python上海培训哪里比较好-上海十大python培训机构排名
  3. 面试问题背后的“猫腻”
  4. 1.2w 星!火爆 GitHub 的 Python 学习 100 天
  5. EL表达式,JSP内置对象
  6. WinForm------如何修改PanelControl控件背景色
  7. 安装centos 6.5 在惠普 ...pro(有惠普增霸卡)上的诸些问题
  8. GlusterFS 4.0开发计划解读
  9. 怎么使用手机号申请邮箱,注册移动手机邮箱有哪些步骤?
  10. 基于node.js的在线聊天室
  11. 启动docker容器时报iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport错误
  12. 攻防世界web练习5
  13. 深入了解Element Form表单动态验证问题
  14. HyperLPR车牌识别技术算法之车牌精定位
  15. 神经网络与深度学习一 :介绍深度学习
  16. 数据,模型,算法共同决定深度学习模型效果
  17. CV/PR:模式识别与图像处理笔试题
  18. 清爽的空气中流动着一种微熏的温情
  19. 网线接无线路由器LAN口,让手机通过WIFI上网
  20. 大数据治理平台架构技术方案(ppt)

热门文章

  1. nginx开启https功能
  2. DDD 领域驱动设计 - 架构(分层/六边形/RESTful)
  3. 原BEA全球副总裁沈惠中出任普元软件CEO
  4. 浅析英语五大成分-主谓宾定状
  5. 浏览器是指在用户计算机,2016年计算机二级office模拟试卷及答案
  6. 10个python接私活的平台,整整10个!总有适合你的,你有技术就有钱
  7. C语言:添加和显示,数据保存在文件中,下次打开可以获取之前录入的内容
  8. 西北农林科技大学linux实验,2021双非科班调剂985(一志愿中科大,调剂上岸西北农林科技大学)初试复试经验帖...
  9. 在c语言中输出8进制数,16进制数
  10. 关于前端调用微信二维码支付,二维码无法显示的问题