This repository was archived by the owner on Aug 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRFKeyboard.m
177 lines (147 loc) · 6.36 KB
/
RFKeyboard.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
#import "RFKeyboard.h"
#import "dout.h"
@interface RFKeyboard () <
UIGestureRecognizerDelegate
>
@property (strong, nonatomic) UITapGestureRecognizer *tapInWindowGestureRecognizer;
@property (strong, nonatomic) UIPanGestureRecognizer *panInWindowGestureRecognizer;
@property (strong, nonatomic) id keyboardShowObserver;
@property (strong, nonatomic) id keyboardHideObserver;
@property (assign, nonatomic) BOOL enableAutoDisimssKeyboardWhenTouch;
@end
@implementation RFKeyboard
+ (CGRect)convertKeyboardFrame:(CGRect)keyboardRect toView:(UIView *)view {
NSParameterAssert(view);
CGRect keyboardFrameOnScreen = keyboardRect;
// General, keyboardFrameInViewsWindow is equal to keyboardFrameOnScreen. But if you change window´s frame, they may be diffrence.
CGRect keyboardFrameInViewsWindow = [view.window convertRect:keyboardFrameOnScreen fromWindow:nil];
return [view convertRect:keyboardFrameInViewsWindow fromView:nil];
}
+ (BOOL)isUndocked:(CGRect)keyboardRect {
// We use a trick here, instead of checking the distance between keyboard buttom and screen buttom.
int zeroCount = 0;
if (keyboardRect.origin.x) zeroCount++;
if (keyboardRect.origin.y) zeroCount++;
if (keyboardRect.size.width) zeroCount++;
if (keyboardRect.size.height) zeroCount++;
return (zeroCount != 2)? YES : NO;
}
+ (CGFloat)keyboardLayoutHeightForNotification:(NSNotification *)note inView:(UIView *)view {
if (!note.userInfo[UIKeyboardFrameEndUserInfoKey]) {
dout_warning(@"Cannot get keyboard frame info for %@", note);
return -1;
}
CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
if (![RFKeyboard isUndocked:frame]) {
return 0;
}
CGRect normalizedFrame = [self convertKeyboardFrame:frame toView:view];
return normalizedFrame.size.height;
}
+ (instancetype)defaultManager {
static RFKeyboard *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
#pragma mark - experimental below
+ (void)setEnableAutoDisimssKeyboardWhenTouch:(BOOL)enabled {
[[RFKeyboard defaultManager] setEnableAutoDisimssKeyboardWhenTouch:enabled];
}
- (void)setEnableAutoDisimssKeyboardWhenTouch:(BOOL)enabled {
@synchronized(self) {
if (enabled) {
if (![self isObservingKeyboardNotification]) {
if (!self.tapInWindowGestureRecognizer.view || !self.panInWindowGestureRecognizer.view) {
[self setupKeyWindowGestureRecognizers];
}
@weakify(self);
self.keyboardShowObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
@strongify(self);
CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
if (![RFKeyboard isUndocked:frame]) {
return;
}
self.tapInWindowGestureRecognizer.enabled = YES;
self.panInWindowGestureRecognizer.enabled = YES;
[self tryAddGestureRecognizerToKeyWindow];
if (self.keyboardShowCallback) {
self.keyboardShowCallback(note);
}
}];
self.keyboardHideObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
@strongify(self);
self.tapInWindowGestureRecognizer.enabled = NO;
self.panInWindowGestureRecognizer.enabled = NO;
if (self.keyboardHideCallback) {
self.keyboardHideCallback(note);
}
}];
}
}
else {
if ([self isObservingKeyboardNotification]) {
[[NSNotificationCenter defaultCenter] removeObserver:self.keyboardShowObserver];
[[NSNotificationCenter defaultCenter] removeObserver:self.keyboardHideObserver];
self.keyboardShowObserver = nil;
self.keyboardHideObserver = nil;
}
}
_enableAutoDisimssKeyboardWhenTouch = enabled;
}
}
- (BOOL)isObservingKeyboardNotification {
return (self.keyboardHideObserver && self.keyboardShowObserver);
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIControl class]]) {
return NO;
}
if ([touch.view isKindOfClass:[UINavigationBar class]]) {
return NO;
}
return YES;
}
- (void)setupKeyWindowGestureRecognizers {
if (self.tapInWindowGestureRecognizer.view && self.panInWindowGestureRecognizer.view) {
return;
}
if (!self.tapInWindowGestureRecognizer) {
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchInKeyWindow:)];
gr.enabled = NO;
gr.cancelsTouchesInView = NO;
gr.delegate = self;
self.tapInWindowGestureRecognizer = gr;
}
if (!self.panInWindowGestureRecognizer) {
UIPanGestureRecognizer *gr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchInKeyWindow:)];
gr.maximumNumberOfTouches = 1;
gr.enabled = NO;
gr.cancelsTouchesInView = NO;
gr.delegate = self;
self.panInWindowGestureRecognizer = gr;
}
if (![self tryAddGestureRecognizerToKeyWindow]) {
dispatch_after_seconds(1, ^{
if (![self tryAddGestureRecognizerToKeyWindow]) {
dout_warning(@"RFKeyboard: Cannot find root window to add gesture recognizer.");
}
});
}
}
- (BOOL)tryAddGestureRecognizerToKeyWindow {
UIWindow *target = [UIApplication sharedApplication].keyWindow;
if (target) {
[target addGestureRecognizer:self.tapInWindowGestureRecognizer];
[target addGestureRecognizer:self.panInWindowGestureRecognizer];
return YES;
}
return NO;
}
- (void)onTouchInKeyWindow:(UIGestureRecognizer *)sender {
// Dismiss keyboard
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
@end