其實(shí),上天給了我十萬條理由讓我去使用系統(tǒng)的導(dǎo)航欄,然而,僅僅只因?yàn)橐粭l,我不得不放棄,我們坑爹的產(chǎn)品經(jīng)理不喜歡。。。
于是,我走上了一條自定義導(dǎo)航欄(花樣作死)
的不歸路。。。
一、自定義滑動(dòng)返回手勢(shì)與滑動(dòng)動(dòng)畫
代碼有點(diǎn)多,為了大家能看懂,我全放在這里,相信同學(xué)們看完都能自己寫出來,建議去github下個(gè)demo,
下載地址:https://github.com/wangzhaomeng/LLNavigationController.git
我實(shí)現(xiàn)了個(gè)簡(jiǎn)單的動(dòng)畫效果,如下圖:
首先說一下思路:
- 重寫push方法,在每一次push的時(shí)候,對(duì)當(dāng)前屏幕進(jìn)行截圖,保存到一個(gè)數(shù)組中
- 重寫pop的一系列方法,在每一次pop的時(shí)候,移除數(shù)組中相對(duì)應(yīng)的截圖
- 自定義返回手勢(shì),滑動(dòng)的時(shí)候,將數(shù)組中的最后一張圖片,添加到當(dāng)前視圖的下方,產(chǎn)生了上一個(gè)視圖在當(dāng)前視圖下方的錯(cuò)覺
思路很簡(jiǎn)單,不過實(shí)現(xiàn)起來,還需要些技巧
下面上代碼:
1、首先,創(chuàng)建一個(gè)簡(jiǎn)單的蒙版
#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、創(chuàng)建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; //保存截屏的數(shù)組
@property (nonatomic, strong) LLNavControllerDelegate *transitionDelagate;
@end
@implementation LLBaseNavigationController
- (void)loadView{
[super loadView];
//self.interactivePopGestureRecognizer.delegate = self; //系統(tǒng)的返回手勢(shì)代理
self.interactivePopGestureRecognizer.enabled = NO; //屏蔽系統(tǒng)的返回手勢(shì)
self.transitionDelagate = [[LLNavControllerDelegate alloc] init];
self.transitionDelagate.presentTransition = @"LLPresentAnimation"; //自定義push動(dòng)畫
self.transitionDelagate.dismissTransition = @"LLDismissAnimation"; //自定義pop動(dòng)畫
self.delegate = self.transitionDelagate;
}
- (void)viewDidLoad{
[super viewDidLoad];
UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
popRecognizer.delegate = self;
[self.view addGestureRecognizer:popRecognizer]; //自定義的滑動(dòng)返回手勢(shì)
self.popRecognizerEnable = YES; //默認(rèn)相應(yīng)自定義的滑動(dòng)返回手勢(shì)
}
#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個(gè)子控制器,停止拖拽
if (self.viewControllers.count <= 1) return;
//在x方向上移動(dòng)的距離
CGFloat tx = [recognizer translationInView:self.view].x;
//在x方向上移動(dòng)的距離除以屏幕的寬度
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){
//移動(dòng)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;
}];
}
}
}
//保存截屏的數(shù)組
- (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
//手勢(shì)代理
- (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) {//設(shè)置手勢(shì)觸發(fā)區(qū)
return YES;
}
}
return NO;
}
//是否與其他手勢(shì)共存,一般使用默認(rèn)值(默認(rèn)返回NO:不與任何手勢(shì)共存)
- (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、創(chuàng)建UINavigationController的擴(kuò)展類<為了更方便的管理手勢(shì)>
#import <UIKit/UIKit.h>
@interface UINavigationController (LLAddPart)
#pragma mark - 為系統(tǒng)類擴(kuò)展屬性
//是否響應(yīng)自定義的滑動(dòng)返回手勢(shì)
- (void)setPopRecognizerEnable:(BOOL)popRecognizerEnable;
- (BOOL)popRecognizerEnable;
//自定義的滑動(dòng)返回手勢(shì)是否與其他手勢(shì)共存,一般使用默認(rèn)值(默認(rèn)返回NO:不與任何手勢(shì)共存)
- (void)setRecognizeSimultaneouslyEnable:(BOOL)recognizeSimultaneouslyEnable;
- (BOOL)recognizeSimultaneouslyEnable;
#pragma mark
@end
#import "UINavigationController+LLAddPart.h"
#import <objc/runtime.h>
@implementation UINavigationController (LLAddPart)
#pragma mark - 為系統(tǒng)類擴(kuò)展屬性
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、集成到項(xiàng)目中,在AppDelegate.h中聲明一個(gè)屬性<蒙版>和一個(gè)類方法,如下:
@property (nonatomic, strong) LLScreenShotView *screenShotView;
+ (instancetype)shareDelegete;
在AppDelegate.m中實(shí)現(xiàn),如下:
- (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;
}
下面說一下易出錯(cuò)的地方:
- 截圖應(yīng)該添加在什么位置合適,首選主window上,添加到主window的最下方,可以用insert方法,添加到第0個(gè)位置。如果不顯示,要看看rootViewController上,有沒有其他的不透明的控件擋住了,也可以添加到rootViewController.view的最下方。
- 截屏之前,要做一個(gè)判斷,保證每一個(gè)視圖控制器只能截屏一次,避免了連點(diǎn)兩下"下一頁"后截屏數(shù)組中添加了兩張截圖,導(dǎo)致畫面不一致
- 手勢(shì)共存問題,最常見的就是與tableView的滑動(dòng)刪除沖突,建議去看下demo,不懂得,再去網(wǎng)上查一下手勢(shì)共存的處理
二、自定義轉(zhuǎn)場(chǎng)動(dòng)畫
<網(wǎng)上找了好多,代碼都不全,所以我把我的代碼全放了出來,供大家參考,上面有demo鏈接,也可以去下載>
1、自定義一個(gè)類,實(shí)現(xiàn)協(xié)議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動(dòng)畫
if(self.presentTransition){
Class transition = NSClassFromString(self.presentTransition);
return [transition new];
}
}
else if (operation == UINavigationControllerOperationPop) {//pop動(dòng)畫
if(self.dismissTransition){
Class transition = NSClassFromString(self.dismissTransition);
return [transition new];
}
}
return nil;
}
@end
2、自定義轉(zhuǎn)場(chǎng)動(dòng)畫
//push動(dòng)畫
#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動(dòng)畫
#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動(dòng)畫
self.transitionDelagate.dismissTransition = @"LLDismissAnimation"; //自定義pop動(dòng)畫
self.delegate = self.transitionDelagate;
OK,大功告成,至于想要什么要的動(dòng)畫效果,各憑所需。。。
此導(dǎo)航繼承于系統(tǒng)類UINavigationController,因此無需考慮性能問題,完全延續(xù)系統(tǒng)的push和pop方法,集成簡(jiǎn)單,同時(shí),無任代碼耦合度,可隨時(shí)從項(xiàng)目中剝離。兩個(gè)項(xiàng)目已上架,目前版本已十分完善,可放心使用。
覺得好,請(qǐng)給個(gè)star,謝謝!