在整理以前项目时,对之前对键盘处理不是很满意,今天特意重新写了一个。
- (void)becomeObserver {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
// 键盘通知
[notificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[notificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[notificationCenter addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark - keyboard Notification
/**键盘隐藏*/
- (void)keyboardWillHide:(NSNotification *)notification {
self.showsKeyboard = NO;
if (!self.showsExtraboard && !self.showsEmojiboard) {
CGFloat fallingHeight = keyboardHeight;
for (UIView *v in self.rootView.subviews) {
if (![v isEqual:self.rootView.chatNavigationBar] && ![v isEqual:self.rootView.chatTableView]) {
CGRect frame = v.frame;
frame.origin.y += fallingHeight;
[UIView animateWithDuration:boardDuration animations:^{
v.frame = frame;
}];
}
if ([v isEqual:self.rootView.chatTableView]) {
CGRect frame = v.frame;
frame.size.height += fallingHeight;
[UIView animateWithDuration:boardDuration animations:^{
v.frame = frame;
}];
}
}
}
}
/**键盘显示*/
- (void)keyboardWillShow:(NSNotification *)notification {
NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect begin = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
if (duration == 0) {
duration = 0.25;
}
if (!self.showsKeyboard) {
// 第三方键盘三次回调,仅执行左后一次(仅以搜狗输入法作的测试)
if (begin.size.height > 0 && begin.origin.y - end.origin.y > 0) {
keyboardHeight = end.size.height;
CGFloat liftingHeight = keyboardHeight;
if (self.showsExtraboard | self.showsEmojiboard) {
liftingHeight = liftingHeight > BOARD_HEIGHT ? liftingHeight - BOARD_HEIGHT : BOARD_HEIGHT - liftingHeight;
}
for (UIView *v in self.rootView.subviews) {
if (![v isEqual:self.rootView.chatNavigationBar] && ![v isEqual:self.rootView.chatTableView]) {
CGRect frame = v.frame;
frame.origin.y -= liftingHeight;
[UIView animateWithDuration:boardDuration - 0.08 animations:^{
v.frame = frame;
}];
}
if ([v isEqual:self.rootView.chatTableView]) {
CGRect frame = v.frame;
frame.size.height -= liftingHeight;
[UIView animateWithDuration:boardDuration - 0.08 animations:^{
v.frame = frame;
}];
}
}
self.showsKeyboard = YES;
self.showsEmojiboard = NO;
self.showsExtraboard = NO;
}
}
}
/**在使用过程中切换键盘*/
- (void)keyboardChangeFrame:(NSNotification *)notification {
if (self.showsKeyboard) {
CGRect begin = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 在ip6s中使用搜狗输入法和系统键盘切换时发现
// sougou --> sys : 282 -> 258
// sys --> sougou : 258 -> 0 -> 216 -> 282 也就是说系统键盘先下去隐藏,再弹出搜狗,在弹出搜狗的过程中在216处有回调
CGFloat offset = end.size.height - begin.size.height;
keyboardHeight = end.size.height;
for (UIView *v in self.rootView.subviews) {
if (![v isEqual:self.rootView.chatNavigationBar] && ![v isEqual:self.rootView.chatTableView]) {
CGRect frame = v.frame;
frame.origin.y -= offset;
[UIView animateWithDuration:0.1 animations:^{
v.frame = frame;
}];
}
if ([v isEqual:self.rootView.chatTableView]) {
CGRect frame = v.frame;
frame.size.height -= offset;
[UIView animateWithDuration:0.1 animations:^{
v.frame = frame;
}];
}
}
}
}