通过NSNotification来监听键盘弹出和弹回
2015-11-01 22:55
211 查看
在通知中心建立一个广播来监听键盘的弹出和弹回,在监听事件中加入触发事件的一些操作。
监听键盘的一些通知:
在这里我需要实现的效果(如下图)是在在键盘弹出时,使下方的toolbar向上移动到相应位置,因此需要知道键盘的高度和弹出动画的时间,通过广播监听来得到键盘的frame和弹出动画时间:
再通过动画效果,改变tableview和toolbar的frame,使得键盘在弹出时不会被遮挡:
效果图:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardwillhide:) name:UIKeyboardWillHideNotification object:nil];
监听键盘的一些通知:
// 键盘的frame发生改变时发出的通知(位置和尺寸) // UIKeyboardWillChangeFrameNotification // UIKeyboardDidChangeFrameNotification // 键盘显示时发出的通知 // UIKeyboardWillShowNotification // UIKeyboardDidShowNotification // 键盘隐藏时发出的通知 // UIKeyboardWillHideNotification // UIKeyboardDidHideNotification
在这里我需要实现的效果(如下图)是在在键盘弹出时,使下方的toolbar向上移动到相应位置,因此需要知道键盘的高度和弹出动画的时间,通过广播监听来得到键盘的frame和弹出动画时间:
NSString *duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]; CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
再通过动画效果,改变tableview和toolbar的frame,使得键盘在弹出时不会被遮挡:
[UIView animateWithDuration:[duration doubleValue] delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ _tableview.frame = CGRectMake(0, 64 , SIZE.width, SIZE.height - 64 - keyboardFrame.size.height - 50); footView.frame = CGRectMake(0, SIZE.height - keyboardFrame.size.height - 50, SIZE.width, 50); } completion:^(BOOL finished) { NSIndexPath *path = [NSIndexPath indexPathForRow:_dataArray.count - 1 inSection:0]; [_tableview scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES]; }];
效果图:
相关文章推荐
- RAID学习笔记
- linux中缺少某些命令该怎么办呢?------以tree命令为例
- 试用了下ITIL开源管理工具iTop,感觉很爽,准备尽快部署并首先实现公司CMDB治理
- Eclipse – SimpleTagSupport was not found on the Java Build Path
- Makefile 自动生成依赖
- 网站安全考虑:1、sql注入 2、跨站脚本攻击
- C++中的类所占内存空间总结
- JAVA设计模式(20) —<行为型>状态模式(State)
- 第九章 触发器
- linux下动态链接实现原理
- Spring AOP进行日志记录
- Transform
- 在字符串中找出第一个只出现一次的字符
- 大道至简第五章读后感
- 快门
- 当今app行业 比较流行的 简称 汇总
- eclipse快捷键
- virtual与abstract的区别
- 拆装箱及文件流
- 关于汇编-1