其实,上天给了我十万条理由让我去使用系统的导航栏,然而,仅仅只因为一条,我不得不放弃,我们坑爹的产品经理不喜欢。。。
于是,我走上了一条自定义导航栏(花样作死)
的不归路。。。
一、自定义滑动返回手势与滑动动画
代码有点多,为了大家能看懂,我全放在这里,相信同学们看完都能自己写出来,建议去github下个demo,
下载地址:https://github.com/wangzhaomeng/LLNavigationController.git
我实现了个简单的动画效果,如下图:
首先说一下思路:
- 重写push方法,在每一次push的时候,对当前屏幕进行截图,保存到一个数组中
- 重写pop的一系列方法,在每一次pop的时候,移除数组中相对应的截图
- 自定义返回手势,滑动的时候,将数组中的最后一张图片,添加到当前视图的下方,产生了上一个视图在当前视图下方的错觉
思路很简单,不过实现起来,还需要些技巧
下面上代码:
1、首先,创建一个简单的蒙版
#import <UIKit/UIKit.h>
@interface LLScreenShotView : UIView
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *maskView;
@end
#import "LLScreenShotView.h"
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds
@implementation LLScreenShotView
- (id)init{
self = [super initWithFrame:SCREEN_BOUNDS];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:SCREEN_BOUNDS];
[self addSubview:_imageView];
_maskView = [[UIView alloc] initWithFrame:SCREEN_BOUNDS];
_maskView.backgroundColor = [UIColor blackColor];
[self addSubview:_maskView];
}
return self;
}
@end
2、创建UINavigationController的子类
#import <UIKit/UIKit.h>
#import "UINavigationController+LLAddPart.h"
@interface LLBaseNavigationController : UINavigationController
@end
#import "LLBaseNavigationController.h"
#import "LLNavControllerDelegate.h"
#import "AppDelegate.h"
@interface LLBaseNavigationController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) NSMutableArray<UIImage *> *childVCImages; //保存截屏的数组
@property (nonatomic, strong) LLNavControllerDelegate *transitionDelagate;
@end
@implementation LLBaseNavigationController
- (void)loadView{
[super loadView];
//self.interactivePopGestureRecognizer.delegate = self; //系统的返回手势代理
self.interactivePopGestureRecognizer.enabled = NO; //屏蔽系统的返回手势
self.transitionDelagate = [[LLNavControllerDelegate alloc] init];
self.transitionDelagate.presentTransition = @"LLPresentAnimation"; //自定义push动画
self.transitionDelagate.dismissTransition = @"LLDismissAnimation"; //自定义pop动画
self.delegate = self.transitionDelagate;
}
- (void)viewDidLoad{
[super viewDidLoad];
UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
popRecognizer.delegate = self;
[self.view addGestureRecognizer:popRecognizer]; //自定义的滑动返回手势
self.popRecognizerEnable = YES; //默认相应自定义的滑动返回手势
}
#pragma mark - 重写父类方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.childViewControllers.count > 0) {
[self createScreenShot];
}
[super pushViewController:viewController animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
[self.childVCImages removeLastObject];
return [super popViewControllerAnimated:animated];
}
- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSArray *viewControllers = [super popToViewController:viewController animated:animated];
if (self.childVCImages.count >= viewControllers.count){
for (int i = 0; i < viewControllers.count; i++) {
[self.childVCImages removeLastObject];
}
}
return viewControllers;
}
- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated{
[self.childVCImages removeAllObjects];
return [super popToRootViewControllerAnimated:animated];
}
- (void)dragging:(UIPanGestureRecognizer *)recognizer{
//如果只有1个子控制器,停止拖拽
if (self.viewControllers.count <= 1) return;
//在x方向上移动的距离
CGFloat tx = [recognizer translationInView:self.view].x;
//在x方向上移动的距离除以屏幕的宽度
CGFloat width_scale;
if (recognizer.state == UIGestureRecognizerStateBegan) {
//添加截图到最后面
width_scale = 0;
[AppDelegate shareDelegete].screenShotView.hidden = NO;
[AppDelegate shareDelegete].screenShotView.maskView.alpha = 0.5;
[AppDelegate shareDelegete].screenShotView.imageView.image = [self.childVCImages lastObject];
}
else if (recognizer.state == UIGestureRecognizerStateChanged){
//移动view
if (tx>10) {
width_scale = (tx-10)/self.view.bounds.size.width;
self.view.transform = CGAffineTransformMakeTranslation(tx-10, 0);
[AppDelegate shareDelegete].screenShotView.maskView.alpha = 0.5-width_scale*0.5;
}
}
else if (recognizer.state == UIGestureRecognizerStateEnded) {
//决定pop还是还原
CGFloat x = [recognizer translationInView:self.view].x;
if (x >= 100) {
[UIView animateWithDuration:0.25 animations:^{
[AppDelegate shareDelegete].screenShotView.maskView.alpha = 0;
self.view.transform = CGAffineTransformMakeTranslation(self.view.bounds.size.width, 0);
} completion:^(BOOL finished) {
[self popViewControllerAnimated:NO];
[AppDelegate shareDelegete].screenShotView.hidden = YES;
self.view.transform = CGAffineTransformIdentity;
}];
} else {
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity;
[AppDelegate shareDelegete].screenShotView.maskView.alpha = 0.5;
} completion:^(BOOL finished) {
[AppDelegate shareDelegete].screenShotView.hidden = YES;
}];
}
}
}
//保存截屏的数组
- (NSMutableArray<UIImage *> *)childVCImages{
if (!_childVCImages) {
_childVCImages = [[NSMutableArray alloc] initWithCapacity:1];
}
return _childVCImages;
}
//截屏
#define WINDOW [UIApplication sharedApplication].delegate.window
- (void)createScreenShot{
if (self.childViewControllers.count == self.childVCImages.count+1) {
UIGraphicsBeginImageContextWithOptions(WINDOW.bounds.size, YES, 0);
[WINDOW.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.childVCImages addObject:image];
}
}
#undef WINDOW
//手势代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if (self.popRecognizerEnable == NO) return NO;
if (self.viewControllers.count <= 1) return NO;
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint point = [touch locationInView:gestureRecognizer.view];
if (point.x < 80.0) {//设置手势触发区
return YES;
}
}
return NO;
}
//是否与其他手势共存,一般使用默认值(默认返回NO:不与任何手势共存)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (self.recognizeSimultaneouslyEnable) {
if ([otherGestureRecognizer isKindOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")] || [otherGestureRecognizer isKindOfClass:NSClassFromString(@"UIPanGestureRecognizer")] ) {
return YES;
}
}
return NO;
}
#pragma mark
@end
3、创建UINavigationController的扩展类<为了更方便的管理手势>
#import <UIKit/UIKit.h>
@interface UINavigationController (LLAddPart)
#pragma mark - 为系统类扩展属性
//是否响应自定义的滑动返回手势
- (void)setPopRecognizerEnable:(BOOL)popRecognizerEnable;
- (BOOL)popRecognizerEnable;
//自定义的滑动返回手势是否与其他手势共存,一般使用默认值(默认返回NO:不与任何手势共存)
- (void)setRecognizeSimultaneouslyEnable:(BOOL)recognizeSimultaneouslyEnable;
- (BOOL)recognizeSimultaneouslyEnable;
#pragma mark
@end
#import "UINavigationController+LLAddPart.h"
#import <objc/runtime.h>
@implementation UINavigationController (LLAddPart)
#pragma mark - 为系统类扩展属性
static BOOL _recognizeSimultaneouslyEnable;
static BOOL _popRecognizerEnable;
- (void)setRecognizeSimultaneouslyEnable:(BOOL)recognizeSimultaneouslyEnable {
NSNumber *t = @(recognizeSimultaneouslyEnable);
objc_setAssociatedObject(self, &_recognizeSimultaneouslyEnable, t, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)recognizeSimultaneouslyEnable {
NSNumber *t = objc_getAssociatedObject(self, &_recognizeSimultaneouslyEnable);
return [t boolValue];
}
- (void)setPopRecognizerEnable:(BOOL)popRecognizerEnable {
NSNumber *t = @(popRecognizerEnable);
objc_setAssociatedObject(self, &_popRecognizerEnable, t, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)popRecognizerEnable {
NSNumber *t = objc_getAssociatedObject(self, &_popRecognizerEnable);
return [t boolValue];
}
#pragma mark
@end
4、集成到项目中,在AppDelegate.h中声明一个属性<蒙版>和一个类方法,如下:
@property (nonatomic, strong) LLScreenShotView *screenShotView;
+ (instancetype)shareDelegete;
在AppDelegate.m中实现,如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *VC = [[ViewController alloc] init];
LLBaseNavigationController *baseNav = [[LLBaseNavigationController alloc] initWithRootViewController:VC];
self.window.rootViewController = baseNav;
return YES;
}
+ (instancetype)shareDelegete{
return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
- (LLScreenShotView *)screenShotView{
if (!_screenShotView) {
_screenShotView = [[LLScreenShotView alloc] init];
_screenShotView.hidden = YES;
[self.window insertSubview:_screenShotView atIndex:0];
}
return _screenShotView;
}
下面说一下易出错的地方:
- 截图应该添加在什么位置合适,首选主window上,添加到主window的最下方,可以用insert方法,添加到第0个位置。如果不显示,要看看rootViewController上,有没有其他的不透明的控件挡住了,也可以添加到rootViewController.view的最下方。
- 截屏之前,要做一个判断,保证每一个视图控制器只能截屏一次,避免了连点两下"下一页"后截屏数组中添加了两张截图,导致画面不一致
- 手势共存问题,最常见的就是与tableView的滑动删除冲突,建议去看下demo,不懂得,再去网上查一下手势共存的处理
二、自定义转场动画
<网上找了好多,代码都不全,所以我把我的代码全放了出来,供大家参考,上面有demo链接,也可以去下载>
1、自定义一个类,实现协议UINavigationControllerDelegate
#import <UIKit/UIKit.h>
@interface LLNavControllerDelegate : NSObject<UINavigationControllerDelegate>
@property (nonatomic, strong) NSString *presentTransition;
@property (nonatomic, strong) NSString *dismissTransition;
@end
#import "LLNavControllerDelegate.h"
@implementation LLNavControllerDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
if (operation == UINavigationControllerOperationPush) {//push动画
if(self.presentTransition){
Class transition = NSClassFromString(self.presentTransition);
return [transition new];
}
}
else if (operation == UINavigationControllerOperationPop) {//pop动画
if(self.dismissTransition){
Class transition = NSClassFromString(self.dismissTransition);
return [transition new];
}
}
return nil;
}
@end
2、自定义转场动画
//push动画
#import <UIKit/UIKit.h>
@interface LLPresentAnimation : NSObject
@end
#import "LLPresentAnimation.h"
@interface LLPresentAnimation ()<UIViewControllerAnimatedTransitioning>
@end
@implementation LLPresentAnimation
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.35f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIView *fromView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toView];
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView transitionFromView:fromView toView:toView duration:duration options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
fromView.transform = CGAffineTransformIdentity;
toView.transform = CGAffineTransformIdentity;
}];
}
@end
--------华丽的分隔符--------
//pop动画
#import <UIKit/UIKit.h>
@interface LLDismissAnimation : NSObject
@end
#import "LLDismissAnimation.h"
@interface LLDismissAnimation ()<UIViewControllerAnimatedTransitioning>
@end
@implementation LLDismissAnimation
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.35f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIView *fromView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toView];
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView transitionFromView:fromView toView:toView duration:duration options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
fromView.transform = CGAffineTransformIdentity;
toView.transform = CGAffineTransformIdentity;
}];
}
@end
3、在自定义的navgationController
里面写上如下代码:
//首先声明属性
@property (nonatomic, strong) LLNavControllerDelegate *transitionDelagate;
//在viewDidLoad中
self.transitionDelagate = [[LLNavControllerDelegate alloc] init];
self.transitionDelagate.presentTransition = @"LLPresentAnimation"; //自定义push动画
self.transitionDelagate.dismissTransition = @"LLDismissAnimation"; //自定义pop动画
self.delegate = self.transitionDelagate;
OK,大功告成,至于想要什么要的动画效果,各凭所需。。。
此导航继承于系统类UINavigationController,因此无需考虑性能问题,完全延续系统的push和pop方法,集成简单,同时,无任代码耦合度,可随时从项目中剥离。两个项目已上架,目前版本已十分完善,可放心使用。
觉得好,请给个star,谢谢!