iPhone和ipad键盘高度及键盘响应事件
2013-11-21 16:49
471 查看
在ios3.2以后的系统中,苹果就提供了键盘使用的api以及demo程序——“KeyboardAccessory”。
处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)
1)在要使用键盘的视图控制器中,接收键盘事件的通知:
2)然后添加键盘事件的处理代码:
获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。
3)在视图控制器消除时,移除键盘事件的通知:
处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)
1)在要使用键盘的视图控制器中,接收键盘事件的通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; // 键盘高度变化通知,ios5.0新增的 #ifdef __IPHONE_5_0 float version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >= 5.0) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil]; } #endif
2)然后添加键盘事件的处理代码:
获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。
#pragma mark - #pragma mark Responding to keyboard events - (void)keyboardWillShow:(NSNotification *)notification { /* Reduce the size of the text view so that it's not obscured by the keyboard. Animate the resize so that it's in sync with the appearance of the keyboard. */ NSDictionary *userInfo = [notification userInfo]; // Get the origin of the keyboard when it's displayed. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. CGRect keyboardRect = [aValue CGRectValue]; // Get the duration of the animation. NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; // Animate the resize of the text view's frame in sync with the keyboard's appearance. [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration]; } - (void)keyboardWillHide:(NSNotification *)notification { NSDictionary* userInfo = [notification userInfo]; /* Restore the size of the text view (fill self's view). Animate the resize so that it's in sync with the disappearance of the keyboard. */ NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration]; }
3)在视图控制器消除时,移除键盘事件的通知:
[[NSNotificationCenter defaultCenter] removeObserver:self];
相关文章推荐
- 『IOS』iPhone和ipad键盘高度及键盘响应事件
- iPhone和ipad键盘高度及键盘响应事件 摘
- iPhone和ipad键盘高度及键盘响应事件
- iPhone和ipad键盘高度及键盘响应事件
- iPhone和ipad键盘高度及键盘响应事件
- iOS键盘高度及键盘响应事件
- 7、osg中响应键盘鼠标事件以及鼠标和键盘编码表
- 也谈VC++.NET2005中DataGrid控件的键盘事件响应
- MFC 对话框Dialog响应键盘事件
- Java 事件响应浅索(Swing,鼠标事件,窗口事件,键盘事件)
- iphone响应地图长按事件 解决长按响应两次的问题
- vc中如何响应键盘和鼠标事件
- Unity3D入门 第贰章 :响应键盘事件
- Henry手记 - Datagrid键盘事件响应(二)
- iPhone iPad 各种控件默认高度
- iphone不支持键盘事件
- Win Form不能响应键盘事件
- iPhone iPad 各种控件默认高度
- 兼容各个浏览器实现键盘响应事件
- as3中解决响应键盘事件时停顿一下的问题,让对象可以八方移动