-
Notifications
You must be signed in to change notification settings - Fork 3
/
StackNavigationController.m
201 lines (154 loc) · 6.64 KB
/
StackNavigationController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// StackNavigationController.m
// StackNavigationController
//
// Created by Igor Kamenev on 25.12.14.
// Copyright (c) 2014 Igor Kamenev. All rights reserved.
//
#import "StackNavigationController.h"
@interface StackNavigationController () <UINavigationControllerDelegate>
@property (nonatomic, assign) BOOL isTransitioning;
@property (nonatomic, strong) NSMutableArray *tasks;
@property (nonatomic, weak) id<UINavigationControllerDelegate> customDelegate;
@end
@implementation StackNavigationController
-(void)viewDidLoad {
[super viewDidLoad];
if (self.delegate) {
self.customDelegate = self.delegate;
}
self.delegate = self;
self.tasks = [NSMutableArray new];
}
// we should save navController.delegate to another property because we need delegate
// to prevent multiple push/pop bug
-(void)setDelegate:(id<UINavigationControllerDelegate>)delegate
{
if (delegate == self) {
[super setDelegate:delegate];
} else {
self.customDelegate = delegate;
}
}
- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
@synchronized(self.tasks) {
if (self.isTransitioning) {
void (^task)(void) = ^{
[self pushViewController:viewController animated:animated];
};
[self.tasks addObject:task];
}
else {
self.isTransitioning = YES;
[super pushViewController:viewController animated:animated];
}
}
}
-(UIViewController *)popViewControllerAnimated:(BOOL)animated
{
@synchronized(self.tasks) {
if (self.isTransitioning) {
void (^task)(void) = ^{
[self popViewControllerAnimated:animated];
};
[self.tasks addObject:task];
return nil;
} else {
self.isTransitioning = YES;
return [super popViewControllerAnimated:animated];
}
}
}
-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
@synchronized(self.tasks) {
if (self.isTransitioning) {
void (^task)(void) = ^{
[self popToViewController:viewController animated:animated];
};
[self.tasks addObject:task];
return nil;
} else {
self.isTransitioning = YES;
return [super popToViewController:viewController animated:animated];
}
}
}
-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
@synchronized(self.tasks) {
if (self.isTransitioning) {
void (^task)(void) = ^{
[self popToRootViewControllerAnimated:animated];
};
[self.tasks addObject:task];
return nil;
} else {
self.isTransitioning = YES;
return [super popToRootViewControllerAnimated:animated];
}
}
}
- (void) runNextTask {
@synchronized(self.tasks) {
if (self.tasks.count) {
void (^task)(void) = self.tasks[0];
[self.tasks removeObjectAtIndex:0];
task();
}
}
}
#pragma mark UINavigationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
self.isTransitioning = NO;
if ([self.customDelegate respondsToSelector:@selector(navigationController:didShowViewController:animated:)]) {
[self.customDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
}
// black magic :)
// if one of push/pop will be without animation - we should place this code to the end of runLoop to prevent bad behavior
[self performSelector:@selector(runNextTask) withObject:nil afterDelay:0.0f];
}
// forward other delegate method to customDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self.customDelegate respondsToSelector:@selector(navigationController:willShowViewController:animated:)]) {
[self.customDelegate navigationController:navigationController willShowViewController:viewController animated:animated];
}
}
- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
{
if ([self.customDelegate respondsToSelector:@selector(navigationControllerSupportedInterfaceOrientations:)]) {
return [self.customDelegate navigationControllerSupportedInterfaceOrientations:navigationController];
}
return 0;
}
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController
{
if ([self.customDelegate respondsToSelector:@selector(navigationControllerPreferredInterfaceOrientationForPresentation:)]) {
return [self.customDelegate navigationControllerPreferredInterfaceOrientationForPresentation:navigationController];
}
return UIInterfaceOrientationUnknown;
}
- (id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController
{
if ([self.customDelegate respondsToSelector:@selector(navigationController:interactionControllerForAnimationController:)]) {
return [self.customDelegate navigationController:navigationController interactionControllerForAnimationController:animationController];
}
return nil;
}
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
if ([self.customDelegate respondsToSelector:@selector(navigationController:animationControllerForOperation:fromViewController:toViewController:)]) {
return [self.customDelegate navigationController:navigationController
animationControllerForOperation:operation
fromViewController:fromVC
toViewController:toVC];
}
return nil;
}
@end