什么是 CYNavigationController ?
CYNavigationController 能够使每个 ViewController 拥有独立的 NavigatorBar,并且包含系统 NavigatorBar 的全部功能,而且能够让使用者轻易地进行转场动画的自定义。现已支持 iPad 和横竖屏切换场景下的应用。
应用场景
- 场景1
采用组件化路由协议结构运行APP,有时可能需要根据URI跳转到任意界面,考虑到当前显示的父级Controller的不确定性,这种业务场景是复杂的,如果当前展示的ViewController不是基于Controller管理的,就只能使用Present方式弹出新页面,而默认的Present动画在一般场景下又显得比较突兀;而且有时,在新的页面不在某个NavigationController管理下,此时我们仍然需要它具有一个导航栏,所以如果我们需要根据各种情况,去用不同的方法来实现相同效果的,容易造成混乱。
CYNavigationController正好提供了这样一套解决方案,在项目中我们可以不嵌套一层导航控制器,而是通过CYAnimatedTransition自定义Present动画模拟Push,用CYCustomedNavigationBar单独为每个页面实现一个导航栏,这样就可以采用统一的方式对包括协议跳转的页面在内的这些页面进行管理,做到同UINavigationController一样的显示效果。
- 场景2
CYAnimatedTransition可以单独集成使用,的自定义拓展性比较强,可以根据需求进行Push和Present动画的自定义,拓展的详细方法下文会有介绍,SDK默认包含一个“神奇效果”动画,一个“push”动画可以直接使用和作为自定义动画的示例参考,动画对象一经创建,在Present和push场景下通用。
- 场景3
CYCustomedNavigationBar也可以单独集成使用,在UINavigationController涉及的某个页面仍然需要一个导航栏,不一定非要为这个页面再单独嵌套一层导航控制器,也不必担心通过自定义View来制作导航栏添加按钮和设置样式的繁琐,CYCustomedNavigationBar可以让你在没有导航控制器支持的ViewController中像使用系统导航控制器一样拥有一个导航栏,并且是自己独立管理的。
CYNavigationControllerSDK包含两部分:
- CYCustomedNavigationBar
CYCustomedNavigationBar可以在不使用UINavigationController时在ViewController中仍然拥有一个独立管理的、可以像使用navigationController时一样设置各种Item的NavigationBar,默认适配iPhoneX。
- CYAnimatedTransition
CYAnimatedTransition是一个可拓展的转场动画库,动画对象同时适用于Push和Present两种新页面显示模式,支持默认手势返回和自定义手势返回。随SDK附赠一对“神奇效果”的动画,一对模仿系统Push的动画。
二者可以结合使用也可以根据需要单独集成某一项。
示例
安装
-
推荐使用CocoaPods安装:pod 'CYCustomedNavigation'
-
考虑到可能的动画代码扩充,集成时请直接Download zip文件,将CYCustomedNavigationSDK目录直接拖进自己的工程目录即可。
用法
CYCustomedNavigationBar
使用CYCustomedNavigationBar时,直接在ViewController中
#import "aViewController.h"
#import "CYCustomedNavigationBar.h"
@interface aViewController ()
- (void)viewDidLoad {
[super viewDidLoad];
//当此页面需要一个导航栏时,需要手动创建
self.cy_navigationBar = [[CYCustomedNavigationBar alloc] init];
//设置导航栏title
self.cy_navigationBar.navigationItem.title = @"present模仿push动画";
//设置导航栏背景颜色
self.cy_navigationBar.barTintColor = [UIColor orangeColor];
//leftItem
self.cy_navigationBar.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithTarget:self action:@selector(backItemClicked)];
}
@end
其中各种Item的颜色、字体等的主题设置已然生效,但是导航栏的背景必须通过cy_navigationBar.barTintColor来设置。
//title
UINavigationBar *appearance = [UINavigationBar appearance];
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:18 weight:2];
[appearance setTitleTextAttributes:textAttrs];
//item
UIBarButtonItem *appearance = [UIBarButtonItem appearance];
[appearance setTintColor:[UIColor whiteColor]];
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
[appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
CYAnimatedTransition
CYAnimatedTransition在应用时也能实现无侵入,每个动画包含正向动画和反向动画两部分,当我们扩展动画库时需要注意,下文将详细介绍,使用时我们的代码中只需要处理正向动画即可
导入主头文件
#import "CYAnimatedTransition.h"
- Present
为ViewController创建一个转场动画对象,并将其设置为ViewController
//自定义present
CYMagicMoveTransition *animatedTransition = [[CYMagicMoveTransition alloc] init];
animatedTransition.sourceViewDataSource = self;
animatedTransition.destinationViewDataSource = detailVC;
animatedTransition.delegate = detailVC;
[detailVC setCY_presentAnimatedTransition:animatedTransition];
然后像平时一样调用present
[self presentViewController:detailVC animated:YES completion:nil];
- Push
创建一个同样的转场动画对象,并将其设置为ViewController
//自定义push
CYMagicMoveTransition *animatedTransition = [[CYMagicMoveTransition alloc] init];
animatedTransition.sourceViewDataSource = self;
animatedTransition.destinationViewDataSource = detailVC;
animatedTransition.delegate = detailVC;
[detailVC setCY_pushAnimatedTransition:animatedTransition forSourceViewController:self];
然后像平时一样调用push
[self.navigationController pushViewController:detailVC animated:YES];
此外,SDK还提供了一个方法,自动为您找到当前显示的页面,并将新页面以push动画效果从当前显示页面Present出来,此方法可以直接应用于组件化中的push模拟。
[detailVC cy_presentFromTopViewControllerWithAnimated:YES];
- 关于sourceViewDataSource和destinationViewDataSource
类似于“神奇效果”动画,有些动画需要在动画过程中将前一个SourceViewController中的一个View移动到DestiationViewController中的另一个位置,为了实现无侵入,因此通过DataSource将这些View传递给动画。
- 关于sourceViewDataSource和destinationViewDataSource
有时,除了SourceViewDataSource提供的View之外,我们可能需要在DestinationViewController中在动画开始时做一些其他UI控件的动画,或者在动画开始或结束时做一些其他的事情,这时可以使用以下两个delegate方法
@protocol CYAnimatedTransitionDelegate <NSObject>
@optional
/*
* Sycn called before animation, you can add some UIViewAnimation for other view in this method and the UIViewAnimation-task's characteristic ,it would seem like to called asycn.
*/
- (void)CYAnimatedTransitionStartAnimatingWithAnimatedTransition:(CYBaseAnimatedTransition *_Nullable)animatedTransition;
- (void)CYAnimatedTransitionEndAnimatingWithAnimatedTransition:(CYBaseAnimatedTransition *_Nullable)animatedTransition;
@end
Expansion
如果需要扩展动画库,请仔细阅读本段内容
CYAnimatedTransition的核心包含三个类CYBaseAnimatedTransition、CYForwardTransition、CYInverseTransition,其中CYBaseAnimatedTransition是核心基类,CYForwardTransition和CYInverseTransition分别是正向和反向动画的基类,其中处理了一些正向和反向动画的区别业务,并在正向动画创建时自动为我们创建一个反向动画并设置数据,我们扩展时应该分别从这两个父类继承正反向动画。
下面将以demo中的CYMagicMoveTransition为例,说明我们在扩展动画库时需要做的工作。
- 1.新建类文件
按照Forward中自动帮我们创建反向动画对象的规则,在创建动画类文件时应分别命名为:
正向动画类:XXXTransition
反向动画类:XXXInverseTransition
- 2.内部实现
在动画对象内部重写父类的动画方法,按需实现动画细节,其中的sourceViewController和destinationViewController是转场动画发生时系统自动帮我们获取的,sourceView和destinationView是我们通过DataSource自定义获取到的,在动画结束时,需要调用
[self transitionComplete];
进行收尾处理。在反向动画内部同理,只不过反向动画内部是把dimiss或者pop当做正向。以下为示例:
#import "CYMagicMoveTransition.h"
@interface CYMagicMoveTransition()
@end
@implementation CYMagicMoveTransition
#pragma mark - cover from super-class
- (void)animateTransition {
[super animateTransition];
// Get sourceView, destinationView and create sourceView's snapShot.
UIView *snapShotView = [self.sourceView snapshotViewAfterScreenUpdates:NO];
snapShotView.frame = [self.containerView convertRect:self.sourceView.frame fromView:self.sourceView.superview];
snapShotView.layer.cornerRadius = 20;
//Setup toVC before animating.
self.destinationViewController.view.frame = [self.transitionContext finalFrameForViewController:self.destinationViewController];
self.destinationViewController.view.alpha = 0;
//Start animating.
[self.containerView addSubview:self.destinationViewController.view];
[self.containerView addSubview:snapShotView];
[self.containerView layoutIfNeeded];
self.sourceView.hidden = YES;
self.destinationView.hidden = YES;
[UIView animateWithDuration:[self transitionDuration:self.transitionContext] delay:0.0f usingSpringWithDamping:0.6f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
snapShotView.layer.cornerRadius = 0;
self.destinationViewController.view.alpha = 1.0;
snapShotView.frame = [self.containerView convertRect:self.destinationView.frame fromView:self.destinationView.superview];
} completion:^(BOOL finished) {
self.sourceView.hidden = NO;
self.destinationView.hidden = NO;
[snapShotView removeFromSuperview];
[self transitionComplete];
}];
}
@end
- 关于push和present自定义动画的异同
附上两张结构图:
Hope
- 如果在使用过程中发现任何错误,希望您能提交issue,感谢您或尝试下载此框架的最新代码以查看错误是否已修复。
- 如果在使用过程中发现功能不够用,希望您能提交issue,我非常希望能够添加更多有用的功能到这个框架中,感谢!
- 如果在使用过程中遇到任何问题,请提交issue,我会尽快解决。
- 如果对需求有任何意见或建议,也请提出来。
联系方式
- QQ:397604080 微信:cy397604080
授权协议
MIT许可证(MIT)- 请查看LICENSE文件。