zoukankan      html  css  js  c++  java
  • 键盘事件

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

    - (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; }]; } } } }
  • 相关阅读:
    sqlmap从入门到精通-第四章-4-6 MySQL数据库导入与导出攻略
    Python文章索引(持续更新~)
    如何用 Python 绘制玫瑰图等常见疫情图
    《民国奇探》的弹幕有点逗比,用 Python 爬下来看看
    发现了合自己胃口的公众号,但文章太多翻来翻去真麻烦,还好我学了 Python
    潘粤明的《龙岭迷窟》到底怎么样?我用 Python 得出了一些结论!
    Python 分析电影《南方车站的聚会》
    使用 Scrapy 爬取去哪儿网景区信息
    Python 爬虫(二):Requests 库
    Python 爬虫(一):爬虫伪装
  • 原文地址:https://www.cnblogs.com/buakaw/p/5533198.html
Copyright © 2011-2022 走看看