其實(shí)易遣,上天給了我十萬(wàn)條理由讓我去使用系統(tǒng)的導(dǎo)航欄嫌佑,然而歧强,僅僅只因?yàn)橐粭l,我不得不放棄肤京,我們坑爹的產(chǎn)品經(jīng)理不喜歡茅特。白修。。
于是肯骇,我走上了一條自定義導(dǎo)航欄(花樣作死)
的不歸路笛丙。假颇。笨鸡。
一坦冠、自定義滑動(dòng)返回手勢(shì)與滑動(dòng)動(dòng)畫(huà)
代碼有點(diǎn)多蓝牲,為了大家能看懂泰讽,我全放在這里已卸,相信同學(xué)們看完都能自己寫(xiě)出來(lái)累澡,建議去github下個(gè)demo,
下載地址:https://github.com/wangzhaomeng/LLNavigationController.git
我實(shí)現(xiàn)了個(gè)簡(jiǎn)單的動(dòng)畫(huà)效果奥吩,如下圖:
首先說(shuō)一下思路:
- 重寫(xiě)push方法霞赫,在每一次push的時(shí)候端衰,對(duì)當(dāng)前屏幕進(jìn)行截圖甘改,保存到一個(gè)數(shù)組中
- 重寫(xiě)pop的一系列方法十艾,在每一次pop的時(shí)候,移除數(shù)組中相對(duì)應(yīng)的截圖
- 自定義返回手勢(shì)荤牍,滑動(dòng)的時(shí)候参淫,將數(shù)組中的最后一張圖片愧杯,添加到當(dāng)前視圖的下方力九,產(chǎn)生了上一個(gè)視圖在當(dāng)前視圖下方的錯(cuò)覺(jué)
思路很簡(jiǎn)單跌前,不過(guò)實(shí)現(xiàn)起來(lái),還需要些技巧
下面上代碼:
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的子類(lèi)
#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)畫(huà)
self.transitionDelagate.dismissTransition = @"LLDismissAnimation"; //自定義pop動(dòng)畫(huà)
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 - 重寫(xiě)父類(lèi)方法
- (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ò)展類(lèi)<為了更方便的管理手勢(shì)>
#import <UIKit/UIKit.h>
@interface UINavigationController (LLAddPart)
#pragma mark - 為系統(tǒng)類(lèi)擴(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)類(lèi)擴(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è)類(lèi)方法库说,如下:
@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;
}
下面說(shuō)一下易出錯(cuò)的地方:
- 截圖應(yīng)該添加在什么位置合適,首選主window上嘲叔,添加到主window的最下方抽活,可以用insert方法硫戈,添加到第0個(gè)位置。如果不顯示下硕,要看看rootViewController上丁逝,有沒(méi)有其他的不透明的控件擋住了,也可以添加到rootViewController.view的最下方梭姓。
- 截屏之前霜幼,要做一個(gè)判斷,保證每一個(gè)視圖控制器只能截屏一次糊昙,避免了連點(diǎn)兩下"下一頁(yè)"后截屏數(shù)組中添加了兩張截圖辛掠,導(dǎo)致畫(huà)面不一致
- 手勢(shì)共存問(wèn)題释牺,最常見(jiàn)的就是與tableView的滑動(dòng)刪除沖突萝衩,建議去看下demo,不懂得没咙,再去網(wǎng)上查一下手勢(shì)共存的處理
二猩谊、自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
<網(wǎng)上找了好多,代碼都不全祭刚,所以我把我的代碼全放了出來(lái)牌捷,供大家參考,上面有demo鏈接涡驮,也可以去下載>
1暗甥、自定義一個(gè)類(lèi),實(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)畫(huà)
if(self.presentTransition){
Class transition = NSClassFromString(self.presentTransition);
return [transition new];
}
}
else if (operation == UINavigationControllerOperationPop) {//pop動(dòng)畫(huà)
if(self.dismissTransition){
Class transition = NSClassFromString(self.dismissTransition);
return [transition new];
}
}
return nil;
}
@end
2捉捅、自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
//push動(dòng)畫(huà)
#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)畫(huà)
#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
里面寫(xiě)上如下代碼:
//首先聲明屬性
@property (nonatomic, strong) LLNavControllerDelegate *transitionDelagate;
//在viewDidLoad中
self.transitionDelagate = [[LLNavControllerDelegate alloc] init];
self.transitionDelagate.presentTransition = @"LLPresentAnimation"; //自定義push動(dòng)畫(huà)
self.transitionDelagate.dismissTransition = @"LLDismissAnimation"; //自定義pop動(dòng)畫(huà)
self.delegate = self.transitionDelagate;
OK,大功告成棒口,至于想要什么要的動(dòng)畫(huà)效果寄月,各憑所需。无牵。漾肮。
此導(dǎo)航繼承于系統(tǒng)類(lèi)UINavigationController,因此無(wú)需考慮性能問(wèn)題茎毁,完全延續(xù)系統(tǒng)的push和pop方法克懊,集成簡(jiǎn)單,同時(shí),無(wú)任代碼耦合度保檐,可隨時(shí)從項(xiàng)目中剝離耕蝉。兩個(gè)項(xiàng)目已上架崔梗,目前版本已十分完善夜只,可放心使用。
覺(jué)得好蒜魄,請(qǐng)給個(gè)star扔亥,謝謝!