使用CATransaction实现push pop控制器结束的回调
在我们的iOS开发中,我们或多或少的会出现某些特殊的跳转需求,
A界面push到B界面,然后B再push到C界面,但是我pop的时候我希望返回的是A界面.
咋看起来确实很简单,
系统会有这样的方法popToViewController:animate:
我们可以指定到pop到A界面.
但是这样的简单就结束了吗?
并不是,
现在的很多应用都会开启边缘向右滑动返回,甚至如网易新闻等都是全屏支持滑动返回的.
此时我们在想想会不会不合时宜呢?我们点击返回按钮是pop到指定的A界面,但是滑动返回我们又是能看到C界面下面的仍然是B界面
但是,我们此时是万万不想要B界面存在的.
我们于是又想到了setViewControllers:animated:的方法
只要我们在push完成之后就调用setViewControllers把中间的那个B给移除掉即可.
那么问题又来了,什么时候是移除控制器的最恰当的时候?
系统只给了我们animated,但是没有给finish或者complete的完成通知或者回调.
稍微细想一点,我们可能就会想到UINavigationControllerDelegate中的两个方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
是的我们可以做拦截,我们可以定义在didShowViewController中,判断是不是当前的那个刚刚push的C界面,如果是的话,我们移除B界面
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if ([viewController isKindOfClass:[C class]]) {
NSMutableArray *arr = [self.childViewControllers mutableCopy];
[arr removeObject:B];
self setViewControllers:arr animated:NO;
}
}
于是我们能大致想到这样的代码,
但是我们如果所有的都在此做判断,会不会就有点过分了呢?
完全没有必要.
那么我们能不能自己实现一个complete在原有的系统方法上呢?
比如
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion: (void (^)(void))completion
如果我们实现一个这样的方法,我们我们就可以在complete中干很多事情,移除需要移除的界面,暂停需要暂停的计时器,开启需要开启的计时器等等...
我们无法知道时间,于是使用延迟
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion: (void (^)(void))completion {
[self.navigationController pushViewController:viewController animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(completion){
completion();
}
});
}
这样很简单的实现了所需要的要求,但是我们又怎么保证push或者pop的时间就是0.5秒?
于是我们想到既然push是可以有动画的,那么是否可以进行事务嵌套,使其作为实事务的一部分?
于是想到了CATransaction
于是一个完成的简单封装就顺利完成了
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion: (void (^)(void))completion {
[CATransaction setCompletionBlock:completion];
[CATransaction begin];
[self pushViewController:viewController animated:animated];
[CATransaction commit];
}
是的,就是这么简单
那么我们就继续封装,把系统的几个动画都做一下扩展
//MARK:系统push|pop添加completion方法回调
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion: (void (^)(void))completion {
[CATransaction setCompletionBlock:completion];
[CATransaction begin];
[self pushViewController:viewController animated:animated];
[CATransaction commit];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated completion: (void (^)(void))completion {
UIViewController *poppedViewController;
[CATransaction setCompletionBlock:completion];
[CATransaction begin];
poppedViewController = [self popViewControllerAnimated:animated];
[CATransaction commit];
return poppedViewController;
}
- (NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated completion: (void (^)(void))completion{
NSArray<__kindof UIViewController*> *viewControllers;
[CATransaction setCompletionBlock:completion];
[CATransaction begin];
viewControllers = [self popToViewController:viewController animated:animated];
[CATransaction commit];
return viewControllers;
}
- (NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated completion: (void (^)(void))completion {
NSArray<__kindof UIViewController*> *viewControllers;
[CATransaction setCompletionBlock:completion];
[CATransaction begin];
viewControllers = [self popToRootViewControllerAnimated:animated];
[CATransaction commit];
return viewControllers;
}
简单的封装完成之后,我们就会发现我们做了其实很实用的扩展.
比如我们刚开始的案例,我们可以进一步封装
-(void)popThenPushViewController:(UIViewController *)pushvc animated:(BOOL)animated{
__weak typeof(self)weakSelf = self;
[self pushViewController:pushvc animated:animated completion:^{
__strong typeof(weakSelf)strongSelf = weakSelf;
NSMutableArray *tempArr = [strongSelf.childViewControllers mutableCopy];
if (tempArr.count>2) {
[tempArr removeObjectAtIndex:tempArr.count-2];
[strongSelf setViewControllers:tempArr animated:NO];
}
}];
}
这样我们会先push界面,push完成之后立即把自己给移除,这样push之后的界面,无需做过多判断,只要pop即可,无论是点击返回还是滑动返回,我们无需设置代理,无需设置延迟,无需设置任何其他的多余的全局属性来实现,进而我们再进一步的扩展
-(void)popThenPushViewController:(UIViewController *)pushvc animated:(BOOL)animated{
__weak typeof(self)weakSelf = self;
[self pushViewController:pushvc animated:animated completion:^{
__strong typeof(weakSelf)strongSelf = weakSelf;
NSMutableArray *tempArr = [strongSelf.childViewControllers mutableCopy];
if (tempArr.count>2) {
[tempArr removeObjectAtIndex:tempArr.count-2];
[strongSelf setViewControllers:tempArr animated:NO];
}
}];
}
-(void)popToViewController:(UIViewController *)popvc thenPushViewController:(UIViewController *)pushvc animated:(BOOL)animated{
__weak typeof(self)weakSelf = self;
[self pushViewController:pushvc animated:animated completion:^{
__strong typeof(weakSelf)strongSelf = weakSelf;
NSMutableArray *tempArr = [strongSelf.childViewControllers mutableCopy];
if (tempArr.count>2) {
NSInteger locate = [tempArr indexOfObject:popvc] + 1;
NSInteger length = (tempArr.count - locate - 1);
[tempArr removeObjectsInRange:NSMakeRange(locate, length)];
[strongSelf setViewControllers:tempArr animated:NO];
}
}];
}
-(void)popToViewControllerClass:(Class)popvcClass thenPushViewController:(UIViewController *)pushvc animated:(BOOL)animated{
__weak typeof(self)weakSelf = self;
[self pushViewController:pushvc animated:animated completion:^{
__strong typeof(weakSelf)strongSelf = weakSelf;
NSMutableArray *tempArr = [strongSelf.childViewControllers mutableCopy];
if (tempArr.count>2) {
for (UIViewController *subvc in self.childViewControllers.reverseObjectEnumerator.allObjects) {
if ([subvc isMemberOfClass:popvcClass]) {
NSInteger locate = [tempArr indexOfObject:subvc] + 1;
NSInteger length = (tempArr.count - locate - 1);
[tempArr removeObjectsInRange:NSMakeRange(locate, length)];
[strongSelf setViewControllers:tempArr animated:NO];
break;
}
}
}
}];
}
- (void)popToRootThenPushViewController:(UIViewController *)pushvc animated:(BOOL)animated{
__weak typeof(self)weakSelf = self;
[self pushViewController:pushvc animated:animated completion:^{
__strong typeof(weakSelf)strongSelf = weakSelf;
NSMutableArray *tempArr = [strongSelf.childViewControllers mutableCopy];
if (tempArr.count>2) {
NSInteger locate = 1;
NSInteger length = (tempArr.count - locate - 1);
[tempArr removeObjectsInRange:NSMakeRange(locate, length)];
[strongSelf setViewControllers:tempArr animated:NO];
}
}];
}
- (NSArray<__kindof UIViewController *> *)popToViewControllerClass:(Class)viewControllerClass animated:(BOOL)animated{
return [self popToViewControllerClass:viewControllerClass animated:animated completion:nil];
}
- (NSArray<__kindof UIViewController *> *)popToViewControllerClass:(Class)viewControllerClass animated:(BOOL)animated completion: (void (^)(void))completion{
NSArray<__kindof UIViewController*> *viewControllers;
if ([viewControllerClass isMemberOfClass:[UIViewController class]]) {
for (UIViewController *subvc in self.childViewControllers.reverseObjectEnumerator.allObjects) {
if ([subvc isKindOfClass:viewControllerClass]) {
viewControllers = [self popToViewController:subvc animated:animated completion:completion];
break;
}
}
}
return viewControllers;
}
于是,我们便简单的通过CATransaction来实现了所需要的本来很复杂的需求,
当然我们可以使用runloop,可以使用kvo等等,但是个人觉得这样会不会更好呢,完全的0侵入,我们只需创建一个分类(类别)即可
或许我们从来没有遇到需要知道push pop完成的结束回调,或许我们经常遇到这样的特殊需求,
但是我总觉得,这样的一个扩展是很实用的,当我们需要使用的时候
Demo地址:https://github.com/spicyShrimp/PUSH_POP