[iOS]监控手机虚拟键盘弹出,消失的通知简单使用
2016-01-23 19:07
555 查看
项目中很多时候会遇到弹出的虚拟键盘,遮挡了输入框,或是其他的一些视图,这时候就需要根据键盘的弹出和消失调整视图的坐标,最好的方法就是用系统的通知机制来监控键盘的弹出和消失;
这主要用到了下面的两个通知:
具体用法如下:在viewDidload方法里注册这两个通知:
然后实现他的方法,记得方法要带参数,我们需要从参数中获取键盘的一些信息:
这样我们可以根据获取到的信息,进行一些操作,上面我是做了一个视图上移的动画!!!
对此,我做了一个简单的封装,可一句代码实现监听,含回调处理时间,完整demo下载地址:https://github.com/LQQZYY/KeyboardShowDismissDemo
这主要用到了下面的两个通知:
UIKeyboardWillShowNotification//键盘弹出的通知 UIKeyboardWillHideNotification//键盘消失的通知
具体用法如下:在viewDidload方法里注册这两个通知:
//注册监控键盘弹出.隐藏的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:)name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHidden:)name:UIKeyboardWillHideNotification object:nil];
然后实现他的方法,记得方法要带参数,我们需要从参数中获取键盘的一些信息:
- (void) keyBoardWillShow:(NSNotification*) notification { NSDictionary *userInfo = [notification userInfo]; //获取键盘的高度 NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; //获取键盘弹出的动画时间 CGFloat interval = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue]; CGRect keyboardEndFrame = [aValue CGRectValue]; CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil]; CGFloat keyboardHeight = keyboardFrame.size.height; [UIView animateWithDuration:interval animations:^{ backgroundView.frame = CGRectMake(0,-keyboardHeight/3.0, SCREEN_WIDTH, SCREEN_HEIGHT); }]; } -(void)keyBoardWillHidden:(NSNotification*) notification { NSDictionary *userInfo = [notification userInfo]; CGFloat interval = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue]; [UIView animateWithDuration:interval animations:^{ backgroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); }]; }
这样我们可以根据获取到的信息,进行一些操作,上面我是做了一个视图上移的动画!!!
对此,我做了一个简单的封装,可一句代码实现监听,含回调处理时间,完整demo下载地址:https://github.com/LQQZYY/KeyboardShowDismissDemo
相关文章推荐
- iOS中将汉字转换成拼音的方法
- [iOS]URL编码和解码
- iOS 模拟器不弹出软键盘的解决办法
- 【我所认知的BIOS】系列blog整理 1.23.2016.zip
- Core Animation1-简介
- iOS block在不同界面的回调传值
- iOS 证书Bug The identity used to sign the executable is no longer valid 解决方案
- iOS程序猿之MAC生成SSH keys
- iOS接入支付宝#include <openssl/asn1.h>没有发现 的问题
- IOS开发之ZBarReaderView的使用
- iOS 【资源篇】
- iOS-数据归档
- 正则表达式在iOS中的运用
- ios 中的block应用
- iOS 显示%
- iOS使用AVCaptureSession自定义相机
- IOS内存管理
- iOS中llvm-Obfuscation的配置
- 转:ios的crash框架方法论
- iOS数据保存总结