Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smileEvday/fc563d73a8d519ac6a84 to your computer and use it in GitHub Desktop.
Save smileEvday/fc563d73a8d519ac6a84 to your computer and use it in GitHub Desktop.
//
// SafeNavigationController.h
// SafeNavigationController.h
//
// Created by 利辺羅 on 2014/08/14.
// Copyright (c) 2014年 CyberAgent Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SafeNavigationController : UINavigationController
@end
//
// SafeNavigationController.m
// SafeNavigationController
//
// Created by 利辺羅 on 2014/08/14.
// Copyright (c) 2014年 CyberAgent Inc. All rights reserved.
//
#import "SafeNavigationController.h"
#define timeToWaitBetweenAnimations 0.5
@interface SafeNavigationController ()
@property (nonatomic, strong) NSMutableArray * controllersQueue;
@property (nonatomic) BOOL animateLastQueuedController;
@property (nonatomic) BOOL pushScheduled;
@property (nonatomic, strong) NSDate * lastAnimatedPushDate;
@end
@implementation SafeNavigationController
- (void)awakeFromNib
{
[super awakeFromNib];
self.controllersQueue = [NSMutableArray array];
}
- (void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
NSLog(@"queue %p", viewController);
[self.controllersQueue addObject:viewController];
self.animateLastQueuedController = animated;
if (self.pushScheduled)
return;
// Wait for push animation to finish
NSTimeInterval timeToWait = self.lastAnimatedPushDate ? timeToWaitBetweenAnimations + [self.lastAnimatedPushDate timeIntervalSinceNow] : 0.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((timeToWait > 0.0 ? timeToWait : 0.0) * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^
{
[self pushQueuedControllers];
self.lastAnimatedPushDate = self.animateLastQueuedController ? [NSDate date] : nil;
self.pushScheduled = NO;
});
self.pushScheduled = YES;
}
- (void)pushQueuedControllers
{
for (NSInteger index = 0; index < (NSInteger)self.controllersQueue.count - 1; index++)
{
NSLog(@"push %p", self.controllersQueue[index]);
[super pushViewController:self.controllersQueue[index]
animated:NO];
}
NSLog(@"push %p animated %@", self.controllersQueue.lastObject, self.animateLastQueuedController ? @"YES" : @"NO");
[super pushViewController:self.controllersQueue.lastObject
animated:self.animateLastQueuedController];
[self.controllersQueue removeAllObjects];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment