UINavigationBar手势侧滑、隐藏bar、UIScrollView侧滑返回研究二

使用过程中有卡顿和测试失效的问题,还需要继续研究优化下
上一篇相关文章:iOS侧滑pop返回的第三方整理研究

知识点:

Runtime+分类+property现实属性

前言

当在实际开发中遇到使用系统navigationBar隐藏或显示展示某些页面,总共有以下4种可能:
显示导航栏页面A->显示导航栏页面B
显示导航栏页面A->隐藏导航栏页面B
隐藏导航栏页面A->显示导航栏页面B
隐藏导航栏页面A->隐藏导航栏页面B
在实际开发中,经常很难同时处理好这几种可能,经常会出现导航栏突然闪一下或是进入页面后才隐藏导航栏,有些在侧滑时会导航栏位置是空的或是黑的,显得特别怪异,但FDFullscreenPopGesture却很好的处理了这个难题,现在研究下这个库的实现

用法

在使用FDFullscreenPopGesture这个库时,在需要隐藏系统导航栏的页面的viewDidLoad方法里设置下fd_prefersNavigationBarHidden属性,需要显示导航栏的页面什么都不处理,使用起来非常简单,如下

// 引入处理侧滑pop返回及处理有无navbar的库
#import "UINavigationController+FDFullscreenPopGesture.h"
@interface HomeController ()
@end

@implementation HomeController
#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
}
@end

原理研究

1、在UINavigationController+FDFullscreenPopGesture文件里写了一个UIViewController的分类UINavigationController+FDFullscreenPopGesture,并利用property和Runtime的方式给UIViewController添加fd_prefersNavigationBarHidden属性

@interface UIViewController (FDFullscreenPopGesture)
@property (nonatomic, assign) BOOL fd_prefersNavigationBarHidden;
@end

@implementation UIViewController (FDFullscreenPopGesture)

- (BOOL)fd_prefersNavigationBarHidden
{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setFd_prefersNavigationBarHidden:(BOOL)hidden
{
    objc_setAssociatedObject(self, @selector(fd_prefersNavigationBarHidden), @(hidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

2、对UINavigationController添加一个分类UINavigationController (FDFullscreenPopGesture),使用Runtime的swizzle黑魔法将pushViewController:animated:的实现替换,增加上额外的处理fd_pushViewController:animated:
,在这个增加额外的方法里的主要功能是

  • 2.1、给UINavigationController的interactivePopGestureRecognizer.view添加一个新的手势,这个添加的手势代理是写的另一个类,同时让系统默认的处理侧滑pop返回的手势注册者失效,目的是让重写了navigationItem的backItem后也能响应侧滑返回
if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.fd_fullscreenPopGestureRecognizer]) {
        
        // Add our own gesture recognizer to where the onboard screen edge pan gesture recognizer is attached to.
        [self.interactivePopGestureRecognizer.view addGestureRecognizer:self.fd_fullscreenPopGestureRecognizer];
        
        // Forward the gesture events to the private handler of the onboard gesture recognizer.
        NSArray *internalTargets = [self.interactivePopGestureRecognizer valueForKey:@"targets"];
        id internalTarget = [internalTargets.firstObject valueForKey:@"target"];
        SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
        self.fd_fullscreenPopGestureRecognizer.delegate = self.fd_popGestureRecognizerDelegate;
        [self.fd_fullscreenPopGestureRecognizer addTarget:internalTarget action:internalAction];
        
        // Disable the onboard gesture recognizer.
        self.interactivePopGestureRecognizer.enabled = NO;
    }
  • 2.2、设置当前即将要push的ViewController的当要处理隐藏导航栏时的block,这个方法的逻辑是在push时给设置一个block,如下
__weak typeof(self) weakSelf = self;
    _FDViewControllerWillAppearInjectBlock block = ^(UIViewController *viewController, BOOL animated) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (strongSelf) {
            [strongSelf setNavigationBarHidden:viewController.fd_prefersNavigationBarHidden animated:animated];
        }
    };
    appearingViewController.fd_willAppearInjectBlock = block;

这个block会在viewWillAppear:animated:这个hook的方法里回调,而这个block的逻辑是根据fd_prefersNavigationBarHidden来动态隐藏或显示UINavigationBar,同时节将被隐藏的UIViewController如果没有设置这个block,也会将同样的逻辑设置给这个Controller,保证在UINavigationController的栈里管理的所有UIViewController都有这个block,全部代码如下:

- (void)fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:(UIViewController *)appearingViewController
{
    if (!self.fd_viewControllerBasedNavigationBarAppearanceEnabled) {
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    _FDViewControllerWillAppearInjectBlock block = ^(UIViewController *viewController, BOOL animated) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (strongSelf) {
            [strongSelf setNavigationBarHidden:viewController.fd_prefersNavigationBarHidden animated:animated];
        }
    };
    
    // Setup will appear inject block to appearing view controller.
    // Setup disappearing view controller as well, because not every view controller is added into
    // stack by pushing, maybe by "-setViewControllers:".
    appearingViewController.fd_willAppearInjectBlock = block;
    UIViewController *disappearingViewController = self.viewControllers.lastObject;
    if (disappearingViewController && !disappearingViewController.fd_willAppearInjectBlock) {
        disappearingViewController.fd_willAppearInjectBlock = block;
    }
}
  • 2.3、在UIViewController即将push出新的Controller,当前Controller解决不可见时也会执行一段代码,代码逻辑为如果解决要push出来的代码如果不隐藏导航栏,则设置[self.navigationController setNavigationBarHidden:NO animated:NO]
    全部代码如下:
- (void)fd_viewWillDisappear:(BOOL)animated
{
    // Forward to primary implementation.
    [self fd_viewWillDisappear:animated];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIViewController *viewController = self.navigationController.viewControllers.lastObject;
        if (viewController && !viewController.fd_prefersNavigationBarHidden) {
            [self.navigationController setNavigationBarHidden:NO animated:NO];
        }
    });
}

总结

对代码进行了深入的初步研究后发现,原理是让每个Controller的viewWillAppear:animated:方法里都执行了一遍是否隐藏导航栏的代码逻辑,比如我在BaseViewController里定义了一个lh_hideNavBar熟悉,只要这样调用就会OK,只是FDFullscreenPopGesture使用了分类的方式,另外也添加了更多判断逻辑的代码,我的代码如下

GitHub:TestPopGestureSolution7


吸收了同事的写法、TZScrollViewPopGestureFDFullscreenPopGesture后写了一个比较简单的封装整理,全部代码如下(总共112行,包含侧滑、隐藏navbar、UIScrollView侧滑):

UIViewController+LHNavigationGesture.h

#import <UIKit/UIKit.h>
@interface UIViewController (LHNavigationGesture) <UIGestureRecognizerDelegate>
/// 是否隐藏导航栏
@property (nonatomic,assign) BOOL lh_hideNavBar;
/// 给view添加侧滑返回效果
- (void)lh_addPopGestureToView:(UIView *)view;
@end

UIViewController+LHNavigationGesture.m

#import "UIViewController+LHNavigationGesture.h"
#import <objc/runtime.h>

@implementation UIViewController (LHNavigationGesture)
+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        [self swizzleBarHidden];
        [self swizzlePopGesture];
    });
}
#pragma mark - ******** 支持手势pop侧滑
+ (void)swizzlePopGesture
{
    Method viewDidLoad_originalMethod = class_getInstanceMethod(self, @selector(viewDidLoad));
    Method viewDidLoad_swizzledMethod = class_getInstanceMethod(self, @selector(lh_viewDidLoad));
    method_exchangeImplementations(viewDidLoad_originalMethod, viewDidLoad_swizzledMethod);
}
- (void)lh_viewDidLoad
{
    [self lh_viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
#pragma mark - ******** 支持navigationBar的隐藏现实不突兀
+ (void)swizzleBarHidden
{
    Method viewWillAppear_originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:));
    Method viewWillAppear_swizzledMethod = class_getInstanceMethod(self, @selector(lh_viewWillAppear:));
    method_exchangeImplementations(viewWillAppear_originalMethod, viewWillAppear_swizzledMethod);
}
- (void)lh_viewWillAppear:(BOOL)animated
{
    [self lh_viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:self.lh_hideNavBar animated:animated];
}

- (void)setLh_hideNavBar:(BOOL)lh_hideNavBar
{
    objc_setAssociatedObject(self, @selector(lh_hideNavBar), @(lh_hideNavBar), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)lh_hideNavBar
{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
#pragma mark - ******** 支持UIScrollView侧滑滚动
- (void)lh_addPopGestureToView:(UIView *)view {
    if (!view) return;
    if (!self.navigationController) {
        // 在控制器转场的时候,self.navigationController可能是nil,这里用GCD和递归来处理这种情况
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self lh_addPopGestureToView:view];
        });
    } else {
        UIPanGestureRecognizer *pan = self.lh_popGestureRecognizer;
        if (![view.gestureRecognizers containsObject:pan]) {
            [view addGestureRecognizer:pan];
        }
    }
}

- (UIPanGestureRecognizer *)lh_popGestureRecognizer {
    UIPanGestureRecognizer *pan = objc_getAssociatedObject(self, _cmd);
    if (!pan) {
        
        NSArray *internalTargets = [self.navigationController.interactivePopGestureRecognizer valueForKey:@"targets"];
        id target = [internalTargets.firstObject valueForKey:@"target"];
        SEL action = NSSelectorFromString(@"handleNavigationTransition:");
        pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:action];
        pan.maximumNumberOfTouches = 1;
        pan.delegate = self.navigationController;
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        objc_setAssociatedObject(self, _cmd, pan, OBJC_ASSOCIATION_ASSIGN);
    }
    return pan;
}
@end

#pragma mark  ******** 支持UIScrollView类型侧滑滚动
@interface UINavigationController (LHPopGesturePrivate)
@end

@implementation UINavigationController (LHPopGesture)

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    if ([[self valueForKey:@"_isTransitioning"] boolValue]) {
        return NO;
    }
    if ([self.navigationController.transitionCoordinator isAnimated]) {
        return NO;
    }
    if (self.childViewControllers.count <= 1) {
        return NO;
    }
    
    // 侧滑手势触发位置
    CGPoint location = [gestureRecognizer locationInView:self.view];
    CGPoint offSet = [gestureRecognizer translationInView:gestureRecognizer.view];
    BOOL ret = (0 < offSet.x && location.x <= 40);
    return ret;
}

/// 只有当系统侧滑手势失败了,才去触发ScrollView的滑动
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容