Skip to content

Instantly share code, notes, and snippets.

@rivera-ernesto
Created August 15, 2014 03:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rivera-ernesto/0bc628be1e24ff5704ae to your computer and use it in GitHub Desktop.
Save rivera-ernesto/0bc628be1e24ff5704ae to your computer and use it in GitHub Desktop.
SafeNavigationController which instead of ignoring subsequent push calls handles a "push queue"
//
// 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
@allmyfire
Copy link

怎么用呢??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment