iOS-通过键盘的监听完成底部工具条的黏性移动
2016-06-23 10:12
411 查看
在平时开发中,有时会遇到在底部添加工具条,但偶尔会遇到这个界面有输入框, 需要弹出键盘,当键盘弹出时,这个工具条就被挡住了,个人感觉很不好(强迫症患者), 如果有需要,将工具条随着键盘一起移动,具体请看实际情况。
先看图,这个是在开发中登录注册界面
键盘弹出前键盘弹出后
具体实现过程很简单,原理是通过通知监听键盘的显示和隐藏
核心代码如下:01-注册通知,监听键盘frame的改变
// 键盘的frame(位置)即将改变, 就会发出UIKeyboardWillChangeFrameNotification // 键盘即将弹出, 就会发出UIKeyboardWillShowNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // 键盘即将隐藏, 就会发出UIKeyboardWillHideNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
02-处理键盘frame大小改变所触发的事件
#pragma mark ----------键盘处理---------- // 键盘即将隐藏 - (void)keyboardWillHide:(NSNotification *)notification { // 通过屏幕的宽度判断手机大小,iPhone 5s一下设备不执行此操作 if ([UIScreen mainScreen].bounds.size.width == 320) return; // 1.键盘弹出需要的时间 CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; // 2.工具条返回动画 [UIView animateWithDuration:duration animations:^{ // 让工具条恢复默认状态 self.bottomToolBar.transform = CGAffineTransformIdentity; }]; } // 键盘即将弹出 - (void)keyboardWillShow:(NSNotification *)notification { // 通过屏幕的宽度判断手机设备,iPhone 5s一下设备不弹出 if ([UIScreen mainScreen].bounds.size.width == 320) return; // 1.键盘弹出需要的时间 CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; // 2.工具条移动动画 [UIView animateWithDuration:duration animations:^{ // 取出键盘高度 CGRect keyboardF = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardH = keyboardF.size.height; self.bottomToolBar.transform = CGAffineTransformMakeTranslation(0, - keyboardH); }]; }
当键盘发出通知时,会把 notification.userInfo 这个字典传给我们,我打印出结果,如下:
// notification.userInfo /** UIKeyboardAnimationCurveUserInfoKey = 7; // UIViewAnimationCurve // 动画运动的轨迹参数 UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 键盘弹出或退出时的时间间隔 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}"; // 键盘自己的bounds大小 UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}"; // 键盘开始时的中心点 UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}"; // 键盘结束时的中心点 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}"; // 键盘开始时的frame UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375, 216}}"; // 键盘结束时的frame UIKeyboardIsLocalUserInfoKey = 1; // NS_AVAILABLE_IOS(9_0) // 9.0出来的,查了下,无果 */
其中,第一个参数,是与动画执行的参数有关(UIViewAnimationCurve)
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) { UIViewAnimationCurveEaseInOut, // slow at beginning and end(开始和结束慢,中间快) UIViewAnimationCurveEaseIn, // slow at beginning (开始慢,越来越快) UIViewAnimationCurveEaseOut, // slow at end (开始快,越来越慢) UIViewAnimationCurveLinear // move linear (直线运动,也就是匀速运动) };
注意:既然注册了通知,用完需要移除通知
// 移除通知 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
最后再贴几张图,表明在键盘弹出前后,底部都可以切换按钮
相关文章推荐
- iOS 核心动画Core Animation
- 如何用xcode7.3在ios10的真机上调试代码
- iOS开发之优秀开源框架IQKeyboardManager,零行代码解决键盘遮挡问题,如此简单解决键盘遮挡问题,IQKeyboardManager源码分解
- iOS App开发中Core Data框架基本的数据管理功能小结
- 《Motion Design for iOS》(二十六)
- iOS 加急审核的办法
- iOS根据视图获取根视图控制器
- iOS YSMine 通用设置
- iOS---iphone----ipad 横竖屏监听的方法
- iOS 获取通讯录的数据
- iOS 替换当前ViewController
- 整理 iOS 多线程GCD的使用
- 【代码笔记】iOS-忘记密码选择整体button
- 【代码笔记】iOS-忘记密码选择整体button
- iOS设置圆角及圆形图片
- iOS框架MVC+MVVM结合的实战
- iOS Git的使用方法(sourcetree)
- iOS 中生成随机数的4种方法(rand、random、arc4random、arc4random_uniform)
- [一句秒懂]iOS调用相机和相册-细节化
- [一句秒懂]iOS 正则匹配