-
Notifications
You must be signed in to change notification settings - Fork 37
/
AHKNavigationController.m
104 lines (80 loc) · 3.35 KB
/
AHKNavigationController.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
// Created by Arkadiusz on 01-04-14.
#import "AHKNavigationController.h"
@interface AHKNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate>
/// A Boolean value indicating whether navigation controller is currently pushing a new view controller on the stack.
@property (nonatomic, getter = isDuringPushAnimation) BOOL duringPushAnimation;
/// A real delegate of the class. `delegate` property is used only for keeping an internal state during
/// animations – we need to know when the animation ended, and that info is available only
/// from `navigationController:didShowViewController:animated:`.
@property (weak, nonatomic) id<UINavigationControllerDelegate> realDelegate;
@end
@implementation AHKNavigationController
#pragma mark - NSObject
- (void)dealloc
{
self.delegate = nil;
self.interactivePopGestureRecognizer.delegate = nil;
}
#pragma mark - UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.delegate) {
self.delegate = self;
}
self.interactivePopGestureRecognizer.delegate = self;
}
#pragma mark - UINavigationController
- (void)setDelegate:(id<UINavigationControllerDelegate>)delegate
{
self.realDelegate = delegate != self ? delegate : nil;
[super setDelegate:delegate ? self : nil];
}
- (void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated __attribute__((objc_requires_super))
{
self.duringPushAnimation = YES;
[super pushViewController:viewController animated:animated];
}
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
NSCAssert(self.interactivePopGestureRecognizer.delegate == self, @"AHKNavigationController won't work correctly if you change interactivePopGestureRecognizer's delegate.");
self.duringPushAnimation = NO;
if ([self.realDelegate respondsToSelector:_cmd]) {
[self.realDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
}
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
// Disable pop gesture in two situations:
// 1) when the pop animation is in progress
// 2) when user swipes quickly a couple of times and animations don't have time to be performed
return [self.viewControllers count] > 1 && !self.isDuringPushAnimation;
} else {
// default value
return YES;
}
}
#pragma mark - Delegate Forwarder
// Thanks for the idea goes to: https://github.com/steipete/PSPDFTextView/blob/ee9ce04ad04217efe0bc84d67f3895a34252d37c/PSPDFTextView/PSPDFTextView.m#L148-164
- (BOOL)respondsToSelector:(SEL)s
{
return [super respondsToSelector:s] || [self.realDelegate respondsToSelector:s];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)s
{
return [super methodSignatureForSelector:s] ?: [(id)self.realDelegate methodSignatureForSelector:s];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
id delegate = self.realDelegate;
if ([delegate respondsToSelector:invocation.selector]) {
[invocation invokeWithTarget:delegate];
}
}
@end