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; }]; } } } }
  • 相关阅读:
    【转】 grep 文件报错 “Binary file ... matches”
    JS 将值插入数组中
    Vue 刷新当前页面,并重新加载页面数据
    Vue 获得所选中目标的状态(checked)以及对应目标的数据,并进行相应的操作
    JS --- 如何获取一个对象的类型
    vue-cli项目开发/生产环境代理实现跨域请求+webpack配置开发/生产环境的接口地址
    VUE 处理文本框获焦点高亮
    VUE项目问题之:去掉url中的#/
    关于移动端终极适配解决方案
    VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
  • 原文地址:https://www.cnblogs.com/buakaw/p/5533198.html
Copyright © 2011-2022 走看看