-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSafeUINavigationController.m
More file actions
315 lines (283 loc) · 9.48 KB
/
SafeUINavigationController.m
File metadata and controls
315 lines (283 loc) · 9.48 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// SafeUINavigationController.m
// MotionDemo
//
// Created by Casey Persson on 6/12/14.
//
//
#import "SafeUINavigationController.h"
//////////////////////////////////////////////////////////////////
// Represents an animation operation
typedef enum {
OP_PUSH,
OP_POP,
} OP_TYPE;
@interface Op : NSBlockOperation
@property () OP_TYPE type;
@property () BOOL modal;
@property () UIViewController *vc;
// A ViewController transition is comprised of 2 "Op"s, the operation that starts the transition, and the operation that represents the end of the transition. Each side of the transition has a pointer to the other end via "pairedOp".
@property () Op *pairedOp;
- (id)initWithType:(OP_TYPE)type viewController:(UIViewController *)viewController modal:(BOOL)modal;
@end
@implementation Op
- (id)initWithType:(OP_TYPE)type viewController:(UIViewController *)viewController modal:(BOOL)modal
{
self = [super init];
if (self)
{
self.type = type;
self.vc = viewController;
self.modal = modal;
}
return self;
}
@end
//////////////////////////////////////////////////////////////////
@interface SafeUINavigationController ()
@property () NSOperationQueue *q;
@property () NSMutableArray *finishOps;
@property () UIWindow *touchInterceptor;
@end
@implementation SafeUINavigationController
- (void)setup
{
self.q = [NSOperationQueue mainQueue];
self.finishOps = [[NSMutableArray alloc] init];
self.delegate = self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setup];
}
return self;
}
- (void)setDelegate:(id<UINavigationControllerDelegate>)delegate
{
// This class needs delegate updates, so setting an outside delegate is illegal. This could be fixed, of course, but no sense doing it til we need it.
assert(delegate == self);
[super setDelegate:delegate];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Disable the swipe-to-go-back feature that was introduced in iOS7, as it can fairly easily happen accidentally. When it does, even if you don't swipe it all the way but instead come back to the graph, the uiscrollview's zoom is reset, which is quite annoying. Worse, our didShowViewController delegate function if the user aborts the pop swipe, thus leaving our queue stuck forever waiting for the finish. Disabling this fixes issue #81.
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = NO;
self.touchInterceptor = [[UIWindow alloc] init];
#if defined(SAFE_NAV_DEBUG)
self.touchInterceptor.backgroundColor = [UIColor yellowColor];
self.touchInterceptor.alpha = 0.3;
#endif
}
- (void)disableTaps
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f)
self.touchInterceptor.frame = [UIScreen mainScreen].nativeBounds;
else
self.touchInterceptor.frame = [UIScreen mainScreen].bounds;
self.touchInterceptor.windowLevel = UIWindowLevelAlert;
[self.touchInterceptor makeKeyAndVisible];
}
- (void)enableTaps
{
[self.touchInterceptor resignKeyWindow];
self.touchInterceptor.hidden = YES;
}
// This delegate function tells us when a push or pop has completed.
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// The operation that finished was the first one queued since they're serialized.
Op *op = self.finishOps.firstObject;
[self finishedOp:op];
}
//////////////////////////////////////////////////////////////
// Helpers
- (void)finishedOp:(Op *)op
{
DebugLog(@"Finished a %@ transition.", (op.type == OP_PUSH ? @"PUSH" : @"POP"));
[self.finishOps removeObject:op];
// Disconnect the operations so both can be freed
op.pairedOp.pairedOp = nil;
op.pairedOp = nil;
// Mark the operation as finished. We could probably call [op start] instead.
[self.q addOperation:op];
// The next operation will start if there is one.
[self enableTaps];
}
- (void)doPush:(Op *)pushOp
{
[self disableTaps];
BOOL immediate;
if (self.finishOps.count == 0)
{
immediate = YES;
}
else
{
immediate = NO;
// Can't do the push until the last operation finishes
[pushOp addDependency:self.finishOps.lastObject];
}
Op *pushFinished = [[Op alloc] initWithType:OP_PUSH viewController:pushOp.vc modal:pushOp.modal];
pushOp.pairedOp = pushFinished;
pushFinished.pairedOp = pushOp;
[self.finishOps addObject:pushFinished];
if (immediate)
{
DebugLog(@"Pushing immediately.");
[pushOp start];
}
else
{
DebugLog(@"Scheduling Push.");
[pushFinished addDependency:pushOp];
// Schedule it
[self.q addOperation:pushOp];
}
}
- (void)doPop:(Op *)popOp
{
[self disableTaps];
// Can't do the pop until the last operation finishes
if (popOp.modal)
{
for (NSInteger i=self.finishOps.count-1; i>=0; i--)
{
if (((Op *)self.finishOps[i]).modal)
{
[popOp addDependency:self.finishOps[i]];
break;
}
}
}
else
{
if (self.finishOps.lastObject)
{
[popOp addDependency:self.finishOps.lastObject];
}
}
Op *popFinished = [[Op alloc] initWithType:OP_POP viewController:popOp.vc modal:popOp.modal];
popOp.pairedOp = popFinished;
popFinished.pairedOp = popOp;
[popFinished addDependency:popOp];
[self.finishOps addObject:popFinished];
// Schedule it
DebugLog(@"Scheduling Pop: %@", popOp.vc);
[self.q addOperation:popOp];
}
////////////////////////////////////////////////////////////////////////////
// We must override any method which pushes or pops a view from the navigation stack.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
Op *pushOp = [[Op alloc] initWithType:OP_PUSH viewController:viewController modal:NO];
[pushOp addExecutionBlock:^{
DebugLog(@"Pushing viewController: %@", viewController);
[super pushViewController:viewController animated:animated];
}];
[self doPush:pushOp];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
Op *popOp = [[Op alloc] initWithType:OP_POP viewController:self.topViewController modal:NO];
[popOp addExecutionBlock:^{
DebugLog(@"Popping viewController: %@", self.topViewController);
[super popViewControllerAnimated:animated];
}];
[self doPop:popOp];
return nil;
}
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
Op *popOp = [[Op alloc] initWithType:OP_POP viewController:self.topViewController modal:NO];
[popOp addExecutionBlock:^{
DebugLog(@"Popping to root view controller.");
[super popToRootViewControllerAnimated:animated];
}];
[self doPop:popOp];
return nil;
}
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
Op *popOp = [[Op alloc] initWithType:OP_POP viewController:self.topViewController modal:NO];
[popOp addExecutionBlock:^{
DebugLog(@"Popping to viewController: %@", viewController);
[super popToViewController:viewController animated:animated];
}];
[self doPop:popOp];
return nil;
}
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
Op *pushOp = [[Op alloc] initWithType:OP_PUSH viewController:viewControllerToPresent modal:YES];
// Use weak reference inside the block as not to create a retain cycle. This does mean we must retain the pushOp until completion. This is done by the pariedOp member.
__weak Op *weakPushOp = pushOp;
[pushOp addExecutionBlock:^{
DebugLog(@"Presenting viewController: %@", viewControllerToPresent);
[super presentViewController:viewControllerToPresent animated:flag completion:
^{
if (completion)
completion();
[self finishedOp:weakPushOp.pairedOp];
}];
}];
[self doPush:pushOp];
}
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
Op *popOp = [[Op alloc] initWithType:OP_POP viewController:self.presentedViewController modal:YES];
// Use weak reference inside the block as not to create a retain cycle. This does mean we must retain the popOp until completion. This is done by the pariedOp member.
__weak Op *weakPopOp = popOp;
[popOp addExecutionBlock:^{
DebugLog(@"Dismissing viewController: %@", self.presentedViewController);
[super dismissViewControllerAnimated:flag completion:
^{
if (completion)
completion();
[self finishedOp:weakPopOp.pairedOp];
}];
}];
[self doPop:popOp];
}
- (void)showViewController:(UIViewController *)vc sender:(id)sender
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f)
{
Op *pushOp = [[Op alloc] initWithType:OP_PUSH viewController:vc modal:NO];
[pushOp addExecutionBlock:^{
DebugLog(@"Showing viewController: %@", vc);
[super showViewController:vc sender:sender];
}];
[self doPush:pushOp];
}
}
- (void)showDetailViewController:(UIViewController *)vc sender:(id)sender
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f)
{
Op *pushOp = [[Op alloc] initWithType:OP_PUSH viewController:vc modal:NO];
[pushOp addExecutionBlock:^{
DebugLog(@"Showing detail viewController: %@", vc);
[super showDetailViewController:vc sender:sender];
}];
[self doPush:pushOp];
}
}
@end