美文网首页
iOS 优雅的替换根控制器rootViewController(

iOS 优雅的替换根控制器rootViewController(

作者: 陈友辉 | 来源:发表于2017-12-13 00:03 被阅读82次

总所周知,在实际业务中,经常会有替换根控制器的需求
例如:
1、登录成功后,切换到tabbarController
2、展示完新特性页后,切换到tabbarController
3、掉线后回到登录后
...
但是直接切换rootVC的时候,界面很生硬,非常不合理
但是替换根控制器,又不是切换页面,无法自定义转场动画
因此可以把动画考虑放在UIWindow上
废话不多说 上代码
OC版

UITabBarViewController *tabBar = [[UITabBarViewController alloc] init];
CATransition *transtition = [CATransition animation];
transtition.duration = 0.5;
transtition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[UIApplication sharedApplication].keyWindow.rootViewController = tabBar;
[[UIApplication sharedApplication].keyWindow.layer addAnimation:transtition forKey:@"animation"];

Swift 4.0版

let tabBarController =UITabbarController()
let transtition = CATransition()
transtition.duration = 0.5
transtition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
UIApplication.shared.keyWindow?.layer.add(transtition, forKey: "animation")
UIApplication.shared.keyWindow?.rootViewController = tabBarController

有了这个思路,可以实现更多炫酷的动画,来实现替换根控制器的转场动画

相关文章

网友评论

      本文标题:iOS 优雅的替换根控制器rootViewController(

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