美文网首页iOS知识搜集程序员iOS Developer
FIFO和LIFO自动管理modal控制器

FIFO和LIFO自动管理modal控制器

作者: HJaycee | 来源:发表于2017-04-14 00:06 被阅读389次

    在一个App中,弹窗一直是一个使用频率较高的提示类控件。苹果对用户体验方面的重视程度有多高,在弹窗的处理上就能体现出这一点来。不知你是否留意过新安装的App上的弹窗显示顺序?通常是这样的,如果先出现的是通知权限弹窗然后是定位权限弹窗,前一个弹窗会暂时隐藏,等用户关掉后一个弹窗后才再次弹出来。显然苹果认为后面的弹窗更重要,所以应当优先被用户处理。苹果对弹窗的弹出顺序进行了LIFO(last in, first out)后进先出的管理。

    本文后面会讲解如何使用Runtime多线程实现LIFO和FIFO (first in, last out)先进先出。先看一下最终效果,这是一个自定义转场动画(TransitionAnimation)的UIViewController

    最终效果

    UIAlertView支持自动后进先出管理。但是,它从iOS8开始就已经被废弃了,再次使用UIAlertView,代码中会出现黄色警告提示。以下是UIAlertView被废弃的说明,苹果让我们使用UIAlertController去替代前者。

    UIAlert View is deprecated in iOS 8. (Note that UIAlert View Delegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlert Controller with a preferred Style of UIAlert Controller Style Alert.
    

    UIAlertController是继承于UIViewController并自定义了转场动画的控制器,和其它modal控制器一样调用presentViewController:animated:completion:方法弹出。但是,每个控制器只能拥有一个presentedController,也就是每次只能present一个别的控制器,强行present新的会出现以下提示:

    Warning: Attempt to present <UIAlertController: 0x7fdb635045d0>  on <ViewController: 0x7fdb660090f0> which is already presenting <UIAlertController: 0x7fdb635084a0>
    

    很多情况下,异步请求结束后,我们需要根据服务器的返回信息进行弹窗提示。却无法保证当时的所在控制器是否已经present了别的UIAlertController,新的弹窗是弹不出来的。所以使用UIAlertController会给我们带来很大的困扰。

    还有一种是把自定义的UIView盖到UIWindow的方式进行弹窗。首先这种方式不支持弹出顺序管理,其次同时弹出多个就是多次执行addSubview方法,很多半透明背景遮罩和view叠加在一起,显示效果不言而喻。更重要的是,很多系统控件也使用了window作为父控件,比如键盘的window是UITextEffectsWindow,频繁使用window可能会出现很多意想不到的问题。

    总结一下上述3种弹窗方式:

    弹窗方式 存在的问题
    UIAlertView iOS8开始已经被废弃
    UIAlertController 每次只能弹一个
    -[UIWindow addSubview:] 同时弹出多个的显示效果较差

    现在开始讲解如何用FIFOLIFO管理modal控制器

    先讲相对简单的FIFO

    FIFO流程图

    效果如下

    ps: gif图中,弹窗2的点击事件中弹出弹窗4,观察在FIFO和LIFO中的区别

    自定义UIViewController UIAlertController

    首先,新建一个UIViewController的分类,设计成分类的目的是做到百分百解耦,供控制器调用,并支持对所有继承于UIViewController的控制器进行FIFO和LIFO的modal管理。

    分类的头文件只有一个方法,用来替代系统的presentViewController:animated:completion:方法。该方法接收一个UIViewController参数,一个present完成的回调和dismiss完成的回调。

    // 枚举,要用哪种方式管理modal控制器
    typedef NS_OPTIONS (NSUInteger, JCPresentType) {
        JCPresentTypeLIFO = 0, // last in, first out
        JCPresentTypeFIFO      // first in, last out
    };
    
    // 新的present方法
    - (void)jc_presentViewController:(UIViewController *)controller presentType:(JCPresentType)presentType presentCompletion:(void (^)(void))presentCompletion dismissCompletion:(void (^)(void))dismissCompletion;
    

    .m文件中,方法的实现是这样的

    // 判断JCPresentType枚举类型,跳转到具体方法
    - (void)jc_presentViewController:(UIViewController *)controller presentType:(JCPresentType)presentType presentCompletion:(void (^)(void))presentCompletion dismissCompletion:(void (^)(void))dismissCompletion {
        if (presentType == JCPresentTypeLIFO) {
            [self lifoPresentViewController:controller presentCompletion:presentCompletion dismissCompletion:dismissCompletion];
        } else {
            [self fifoPresentViewController:controller presentCompletion:presentCompletion dismissCompletion:dismissCompletion];
        }
    }
    
    // 核心方法
    - (void)fifoPresentViewController:(UIViewController *)controller presentCompletion:(void (^)(void))presentCompletion dismissCompletion:(void (^)(void))dismissCompletion {
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
            [controller setDeallocCompletion:^{
                if (dismissCompletion) {
                    dismissCompletion();
                }
                // got to next operation
                dispatch_semaphore_signal(semaphore);
            }];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:controller animated:YES completion:presentCompletion];
            });
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        }];
    
        // put in queue
        if ([self getOperationQueue].operations.lastObject) {
            [operation addDependency:[self getOperationQueue].operations.lastObject];
        }
        [[self getOperationQueue] addOperation:operation];
    }
    
    // NSOperationQueue单例,用于添加operation
    - (NSOperationQueue *)getOperationQueue {
        static NSOperationQueue *operationQueue = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            operationQueue = [NSOperationQueue new];
        });
        return operationQueue;
    }
    
    // 使用关联对象存取deallocCompletion这个block
    - (void)setDeallocCompletion:(void (^)(void))completion {
        objc_setAssociatedObject(self, @selector(getDeallocCompletion), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    - (void (^)(void))getDeallocCompletion {
        return objc_getAssociatedObject(self, _cmd);
    }
    
    // hook控制器的viewDidDisappear方法,在这个时候调deallocCompletion这个block
    + (void)load {
        SEL oldSel = @selector(viewDidDisappear:);
        SEL newSel = @selector(jc_viewDidDisappear:);
        Method oldMethod = class_getInstanceMethod([self class], oldSel);
        Method newMethod = class_getInstanceMethod([self class], newSel);
        
        BOOL didAddMethod = class_addMethod(self, oldSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
        if (didAddMethod) {
            class_replaceMethod(self, newSel, method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
        } else {
            method_exchangeImplementations(oldMethod, newMethod);
        }
    }
    
    - (void)jc_viewDidDisappear:(BOOL)animated {
        [self jc_viewDidDisappear:animated];
        
        if ([self getDeallocCompletion] && ![self isTemporarilyDismissed]) {
            [self getDeallocCompletion]();
        }
    }
    

    其中dispatch_semaphore_t是多线程中的信号量,dispatch_semaphore_signal函数使信号量加一,dispatch_semaphore_wait使信号量减一,并且在信号量小于0的时候暂停当前线程。

    presentViewController是一个耗时操作,我把这个操作放在NSBlockOperation中,用dispatch_semaphore_t暂停线程直到present完成。并设置每个NSBlockOperation的前后依赖,最后加到NSOperationQueue中,组成一个串行的先进先出队列。

    下面开始讲LIFO

    LIFO流程图

    效果如下

    自定义UIViewController UIAlertController
    // 核心方法
    - (void)lifoPresentViewController:(UIViewController *)controller presentCompletion:(void (^)(void))presentCompletion dismissCompletion:(void (^)(void))dismissCompletion {
        
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        
        // put in stack
        NSMutableArray *stackControllers = [self getStackControllers];
        if (![stackControllers containsObject:controller]) {
            [stackControllers addObject:controller];
        }
        
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
            __weak typeof(controller) weakController = controller;
            [controller setPresentCompletion:presentCompletion];
            [controller setDismissCompletion:dismissCompletion];
            [controller setDeallocCompletion:^{
                if (dismissCompletion) {
                    dismissCompletion();
                }
                
                // fetch new next controller if exists, because button action after dismiss completion
                [weakController setDismissing:YES];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CGFLOAT_MIN * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakController setDismissing:NO];
                    // if the dismiss controller is the last one
                    if (stackControllers.lastObject == controller) {
                        [stackControllers removeObject:weakController];
                        
                        // is there any previous controllers
                        if (stackControllers.count > 0) {
                            UIViewController *preController = [stackControllers lastObject];
                            [self lifoPresentViewController:preController presentCompletion:[preController getPresentCompletion] dismissCompletion:[preController getDismissCompletion]];
                        }
                    } else {
                        NSUInteger index = [stackControllers indexOfObject:weakController];
                        [stackControllers removeObject:weakController];
                        
                        // is there any next controllers
                        NSArray *nextControllers = [stackControllers objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index, stackControllers.count - index)]];
                        for (UIViewController *nextController in nextControllers) {
                            [self lifoPresentViewController:nextController presentCompletion:[nextController getPresentCompletion] dismissCompletion:[nextController getDismissCompletion]];
                        }
                    }
                });
            }];
            
            // if the previous controller is dismissing, wait it's completion
            if (stackControllers.count > 1) {
                for (UIViewController *preController in stackControllers) {
                    if ([preController isDismissing]) {
                        return ;
                    }
                }
            }
            
            // present a new controller before dismissing the presented controller if exists
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.presentedViewController) {
                    [self.presentedViewController temporarilyDismissViewControllerAnimated:YES completion:^{
                        [self presentViewController:controller animated:YES completion:^{
                            dispatch_semaphore_signal(semaphore);
                        }];
                    }];
                } else {
                    [self presentViewController:controller animated:YES completion:^{
                        dispatch_semaphore_signal(semaphore);
                        if (presentCompletion) {
                            presentCompletion();
                        }
                    }];
                }
            });
            
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        }];
        
        // put in queue
        if ([self getOperationQueue].operations.lastObject) {
            [operation addDependency:[self getOperationQueue].operations.lastObject];
        }
        [[self getOperationQueue] addOperation:operation];
    }
    
    // 使用关联对象存取dismissCompletion
    - (void)setDismissCompletion:(void (^)(void))completion {
        objc_setAssociatedObject(self, @selector(getDismissCompletion), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    - (void (^)(void))getDismissCompletion {
        return objc_getAssociatedObject(self, _cmd);
    }
    // 使用关联对象存取presentCompletion
    - (void)setPresentCompletion:(void (^)(void))completion {
        objc_setAssociatedObject(self, @selector(getPresentCompletion), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    - (void (^)(void))getPresentCompletion {
        return objc_getAssociatedObject(self, _cmd);
    }
    
    // 使用关联对象存取temporarilyDismissed,用于判断是临时隐藏还是用户关闭控制器
    - (void)setTemporarilyDismissed:(BOOL)temporarilyDismissed {
        objc_setAssociatedObject(self, @selector(isTemporarilyDismissed), @(temporarilyDismissed), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (BOOL)isTemporarilyDismissed {
        NSNumber *num = objc_getAssociatedObject(self, _cmd);
        return [num boolValue];
    }
    
    // 使用关联对象存取dismissing
    - (void)setDismissing:(BOOL)dismissing {
        objc_setAssociatedObject(self, @selector(isDismissing), @(dismissing), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (BOOL)isDismissing {
        NSNumber *num = objc_getAssociatedObject(self, _cmd);
        return [num boolValue];
    }
    
    // 数组栈,用于缓存所有传进来的控制器
    - (NSMutableArray *)getStackControllers {
        static NSMutableArray *stackControllers = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            stackControllers = [NSMutableArray array];
        });
        return stackControllers;
    }
    
    // 临时dismiss方法
    - (void)temporarilyDismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion {
        [self setTemporarilyDismissed:YES];
        [self dismissViewControllerAnimated:flag completion:^{
            [self setTemporarilyDismissed:NO];
            if (completion) {
                completion();
            }
        }];
    }
    

    和FIFO方法的区别是,LIFO方法中,每次进来会先判断前面是否已经有控制器了,如果有就先临时dismiss。并且,控制器在被用户关闭的时候,优先判断栈后面有没有还没有弹出来的控制器,然后才判断栈前面有没有控制器。

    这样,一个具有FIFO和LIFO驱动的present分类方法就完成了,敢紧试试吧。


    下载地址:UIViewController+JCPresentQueue.h

    参考资料

    相关文章

      网友评论

      • Beta是条好狗:看完代码感觉你6得不行
        HJaycee:@Beta是条好狗 好的哈!辛苦你了优化了这么多地方,我有空好好看下~
        Beta是条好狗:@HJaycee 实际使用发现了一些问题,Github上的PR看一下 :kissing_heart:
        HJaycee:@Beta是条好狗 谢谢
      • lyuxxx:JCAlertController能定义上面是图片,中间是文字,下面是button的style吗
        lyuxxx:好吧
        HJaycee:不能,你可以自己封装个这样的contentview
      • 共田君:最新的包,调试了下除了FIFO模式 其他方式都不能被释放,麻烦解决一下~
        HJaycee:@共田君 不客气,是我的问题
        共田君:@HJaycee 好的 谢谢了~
        HJaycee:是的,bug我已经修复了,你更新下吧!
      • 荒_a15a:感觉思路很清晰,也解决我之前想用nsoperationqueue实现异步操作的队列,学习了
        HJaycee:@ZhHS https://github.com/HJaycee/JCAlertController/tree/master/JCAlertController/JCAlertController/JCPresentQueue
        ZhHS:你好,现在GitHub上看不了了,可以发一份给我邮箱吗
        HJaycee:对你有帮助就好:blush:

      本文标题:FIFO和LIFO自动管理modal控制器

      本文链接:https://www.haomeiwen.com/subject/rbrpattx.html