键盘事件

Posted Emerys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了键盘事件相关的知识,希望对你有一定的参考价值。

在整理以前项目时,对之前对键盘处理不是很满意,今天特意重新写了一个。

- (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; }]; } } } }

 

以上是关于键盘事件的主要内容,如果未能解决你的问题,请参考以下文章

软输入键盘隐藏编辑文本

pyHook无法监听键盘事件?

键盘事件

在片段替换上显示/隐藏 Android 软键盘

Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段

打开键盘隐藏片段下方的工具栏