Skip to content

Commit

Permalink
Fixed Tencent#627 修复 UITextView、UITextField 内自带的 qmui_keyboardManager…
Browse files Browse the repository at this point in the history
… 无法监听键盘降下的事件的 bug
  • Loading branch information
MoLice committed Jun 25, 2019
1 parent 0b5e44b commit 1283e4d
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions QMUIKit/QMUIComponents/QMUIKeyboardManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ @interface QMUIKeyboardManager ()
@property(nonatomic, assign) CGRect keyboardMoveBeginRect;

@property(nonatomic, weak) UIResponder *currentResponder;
@property(nonatomic, weak) UIResponder *currentResponderWhenResign;
//@property(nonatomic, weak) UIResponder *currentResponderWhenResign;

@property(nonatomic, assign) BOOL debug;

Expand Down Expand Up @@ -88,12 +88,11 @@ + (void)load {
OverrideImplementation([UIResponder class], @selector(resignFirstResponder), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^BOOL(UIResponder *selfObject) {
selfObject.keyboardManager_isFirstResponder = NO;
if (selfObject.isFirstResponder &&
selfObject.qmui_keyboardManager &&
[selfObject.qmui_keyboardManager.allTargetResponders containsObject:selfObject]) {
selfObject.qmui_keyboardManager.currentResponderWhenResign = selfObject;
}

// if (selfObject.isFirstResponder &&
// selfObject.qmui_keyboardManager &&
// [selfObject.qmui_keyboardManager.allTargetResponders containsObject:selfObject]) {
// selfObject.qmui_keyboardManager.currentResponderWhenResign = selfObject;
// }
// call super
BOOL (*originSelectorIMP)(id, SEL);
originSelectorIMP = (BOOL (*)(id, SEL))originalIMPProvider();
Expand Down Expand Up @@ -406,12 +405,16 @@ - (BOOL)isAppActive:(NSNotification *)notification {
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
return NO;
}
if (![[notification.userInfo valueForKey:UIKeyboardIsLocalUserInfoKey] boolValue]) {
if (![[notification.userInfo valueForKey:UIKeyboardIsLocalUserInfoKey] boolValue] && !IS_SPLIT_SCREEN_IPAD) {
return NO;
}
return YES;
}

- (BOOL)isSplitScreenKeyboardNotification {
return IS_SPLIT_SCREEN_IPAD;
}

- (void)keyboardWillShowNotification:(NSNotification *)notification {

if (self.debug) {
Expand Down Expand Up @@ -481,7 +484,7 @@ - (void)keyboardWillHideNotification:(NSNotification *)notification {
return;
}

if (![self shouldReceiveHideNotification]) {
if (![self shouldReceiveHideNotification] && ![self isSplitScreenKeyboardNotification]) {
return;
}

Expand Down Expand Up @@ -514,13 +517,14 @@ - (void)keyboardDidHideNotification:(NSNotification *)notification {
self.lastUserInfo = userInfo;
userInfo.targetResponder = self.currentResponder ?: nil;

if ([self shouldReceiveHideNotification]) {
if ([self shouldReceiveHideNotification] || [self isSplitScreenKeyboardNotification]) {
if (self.delegateEnabled && [self.delegate respondsToSelector:@selector(keyboardDidHideWithUserInfo:)]) {
[self.delegate keyboardDidHideWithUserInfo:userInfo];
}
}

if (self.currentResponder && !self.currentResponder.keyboardManager_isFirstResponder && !IS_IPAD) {
// 时机最晚,设置为 nil
self.currentResponder = nil;
}

Expand All @@ -546,9 +550,9 @@ - (void)keyboardWillChangeFrameNotification:(NSNotification *)notification {
QMUIKeyboardUserInfo *userInfo = [self newUserInfoWithNotification:notification];
self.lastUserInfo = userInfo;

if ([self shouldReceiveShowNotification]) {
userInfo.targetResponder = self.currentResponder ?: nil;
} else if ([self shouldReceiveHideNotification]) {
if ([self shouldReceiveShowNotification] ||
[self shouldReceiveHideNotification] ||
[self isSplitScreenKeyboardNotification]) {
userInfo.targetResponder = self.currentResponder ?: nil;
} else {
return;
Expand Down Expand Up @@ -578,9 +582,9 @@ - (void)keyboardDidChangeFrameNotification:(NSNotification *)notification {
QMUIKeyboardUserInfo *userInfo = [self newUserInfoWithNotification:notification];
self.lastUserInfo = userInfo;

if ([self shouldReceiveShowNotification]) {
userInfo.targetResponder = self.currentResponder ?: nil;
} else if ([self shouldReceiveHideNotification]) {
if ([self shouldReceiveShowNotification] ||
[self shouldReceiveHideNotification] ||
[self isSplitScreenKeyboardNotification]) {
userInfo.targetResponder = self.currentResponder ?: nil;
} else {
return;
Expand All @@ -604,9 +608,8 @@ - (QMUIKeyboardUserInfo *)newUserInfoWithNotification:(NSNotification *)notifica
}

- (BOOL)shouldReceiveShowNotification {
// 这里有BUG,如果点击了webview导致键盘下降,这个时候运行shouldReceiveHideNotification就会判断错误
self.currentResponder = self.currentResponderWhenResign ?: [self firstResponderInWindows];
self.currentResponderWhenResign = nil;
// 这里有 BUG,如果点击了 webview 导致键盘下降,这个时候运行 shouldReceiveHideNotification 就会判断错误,所以如果发现是 nil 则值不变
self.currentResponder = [self firstResponderInWindows] ?: self.currentResponder;
if (self.targetResponderValues.count <= 0) {
return YES;
} else {
Expand Down Expand Up @@ -651,7 +654,9 @@ - (void)keyboardDidChangedFrame:(UIView *)keyboardView {
}

// 也需要判断targetResponder
if (![self shouldReceiveShowNotification] && ![self shouldReceiveHideNotification]) {
if (![self shouldReceiveShowNotification] &&
![self shouldReceiveHideNotification] &&
![self isSplitScreenKeyboardNotification]) {
return;
}

Expand Down

0 comments on commit 1283e4d

Please sign in to comment.