键盘的显示/隐藏-使用总结
2013-07-29 19:52
190 查看
应用中,跟用户交互功能必不可少!
关于键盘的显示/隐藏 中,就隐藏着许多的知识。那我们就来窥看一角。
我们常常抱怨这么几种情况:
1.键盘显示时,遮盖了原来的输入框或者不该遮盖的内容。如何让其他控件不被键盘遮盖?
2.键盘显示的动画与控件的位置移动不一致,导致动画不连贯。
3.相反的,键盘隐藏时,其他控件该如何移动到原来的位置?
等等。
功能:控件根据键盘的显示/隐藏进行重新定位。
贴代码:
1.为键盘的显示/隐藏事件,注册通知。
我们知道:当焦点到可输入的控件上(一般指UITextField)时,键盘会自动显示,并且触发UIKeyboardWillShowNotification通知;当焦点离开可输入的控件时,键盘会自动隐藏,并且触发UIKeyboardWillHideNotification通知。所以,要注册这两种通知事件,在键盘显示/隐藏时,做我们“需要做的操作”。
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
2.键盘的显示事件的回调函数。
- (void)keyboardWillShow:(NSNotification *) notification {
float animationDuration = [[[notification userInfo]valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
CGRect bottomBarFrame = self.mToolBar.frame;
[UIView beginAnimations:@"bottomBarUp" context:nil];
[UIView setAnimationDuration: animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
bottomBarFrame.origin.y = self.view.bounds.size.height - 44 -
height;
[self.mToolBar setFrame:bottomBarFrame];
[UIView commitAnimations];
}
这个方法中,我学到了很多东西。
首先,当事件回调时,我收到了,[notification userInfo]返回的许多有用的数据。
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
根据这些,我能够精确的控制,关于键盘显示时的各种参数,并进行操作。让键盘显示时,让控件也进行相应的移动:[self.mToolBar setFrame:bottomBarFrame];
其次,关于动画的一些知识。简单动画的写法;而且,了解了一些方法的使用:
setAnimationDuration 方法:定义动画的时间。
setAnimationCurve 方法:定义动画的轨迹(动画如何执行)
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
UIViewAnimationCurveEaseInOut, // 开始时慢,中间快,结束时慢
UIViewAnimationCurveEaseIn, // 开始慢,然后加速
UIViewAnimationCurveEaseOut, // 逐渐减速
UIViewAnimationCurveLinear // 匀速
};
3.键盘的隐藏事件的回调函数。
- (void)keyboardWillHide:(NSNotification *) notification {
float animationDuration = [[[notification userInfo]valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGRect bottomBarFrame = self.mToolBar.frame;
[UIView beginAnimations:@"bottomBarDown" context:nil];
[UIView setAnimationDuration: animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
bottomBarFrame.origin.y += height;
[self.mToolBar setFrame:bottomBarFrame];
[UIView commitAnimations];
}
与键盘的显示事件的回调函数相反。我收到了,[notification userInfo]返回的许多有用的数据。
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
知识的海洋,缺少的时发现知识的眼睛!呵呵!
当然,还有ARC的好处,不用手动释放,保证内存不被泄漏。
希望对你有所帮助!
关于键盘的显示/隐藏 中,就隐藏着许多的知识。那我们就来窥看一角。
我们常常抱怨这么几种情况:
1.键盘显示时,遮盖了原来的输入框或者不该遮盖的内容。如何让其他控件不被键盘遮盖?
2.键盘显示的动画与控件的位置移动不一致,导致动画不连贯。
3.相反的,键盘隐藏时,其他控件该如何移动到原来的位置?
等等。
功能:控件根据键盘的显示/隐藏进行重新定位。
贴代码:
1.为键盘的显示/隐藏事件,注册通知。
我们知道:当焦点到可输入的控件上(一般指UITextField)时,键盘会自动显示,并且触发UIKeyboardWillShowNotification通知;当焦点离开可输入的控件时,键盘会自动隐藏,并且触发UIKeyboardWillHideNotification通知。所以,要注册这两种通知事件,在键盘显示/隐藏时,做我们“需要做的操作”。
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
2.键盘的显示事件的回调函数。
- (void)keyboardWillShow:(NSNotification *) notification {
float animationDuration = [[[notification userInfo]valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
CGRect bottomBarFrame = self.mToolBar.frame;
[UIView beginAnimations:@"bottomBarUp" context:nil];
[UIView setAnimationDuration: animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
bottomBarFrame.origin.y = self.view.bounds.size.height - 44 -
height;
[self.mToolBar setFrame:bottomBarFrame];
[UIView commitAnimations];
}
这个方法中,我学到了很多东西。
首先,当事件回调时,我收到了,[notification userInfo]返回的许多有用的数据。
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
根据这些,我能够精确的控制,关于键盘显示时的各种参数,并进行操作。让键盘显示时,让控件也进行相应的移动:[self.mToolBar setFrame:bottomBarFrame];
其次,关于动画的一些知识。简单动画的写法;而且,了解了一些方法的使用:
setAnimationDuration 方法:定义动画的时间。
setAnimationCurve 方法:定义动画的轨迹(动画如何执行)
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
UIViewAnimationCurveEaseInOut, // 开始时慢,中间快,结束时慢
UIViewAnimationCurveEaseIn, // 开始慢,然后加速
UIViewAnimationCurveEaseOut, // 逐渐减速
UIViewAnimationCurveLinear // 匀速
};
3.键盘的隐藏事件的回调函数。
- (void)keyboardWillHide:(NSNotification *) notification {
float animationDuration = [[[notification userInfo]valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGRect bottomBarFrame = self.mToolBar.frame;
[UIView beginAnimations:@"bottomBarDown" context:nil];
[UIView setAnimationDuration: animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
bottomBarFrame.origin.y += height;
[self.mToolBar setFrame:bottomBarFrame];
[UIView commitAnimations];
}
与键盘的显示事件的回调函数相反。我收到了,[notification userInfo]返回的许多有用的数据。
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
知识的海洋,缺少的时发现知识的眼睛!呵呵!
当然,还有ARC的好处,不用手动释放,保证内存不被泄漏。
希望对你有所帮助!
相关文章推荐
- Android 键盘使用三(显示、隐藏、切换、数字键盘、英文数字键盘)
- 软键盘显示及属性android:windowSoftInputMode使用总结 & [转一篇]
- 如何通过代码控制软件键盘的显示与隐藏(InputMethodManager 的使用)
- 总结键盘显示隐藏几种方案
- 同级两个子控件A,B 使用A来实现B的隐藏和显示
- 使用jquery的click事件显示隐藏div
- Android进阶之路 - TextView与EditText单行显示,多行显示,超过限制使用省略号,行距,字距,键盘隐藏与显示
- 01.关于使用Hibernate技术实现分页显示的思考总结
- 无限分级Repeater递归实现:读取一次数据库,使用LINQ2SQL技术,支持排序&显示隐藏
- iOS开发隐藏键盘方法总结
- 关于jquery和zepto.js判断元素的可见性并显示或隐藏总结
- iOS开发隐藏键盘方法总结
- Android 手动显示和隐藏软键盘 android 隐藏显示输入法键盘
- 隐藏Activity刚进来焦点在EditText时显示输入键盘
- android应用程序,使用不同方法,在activity之中隐藏Title,或者实现FullScreen(隐藏通知栏)显示。
- 笔记--键盘的显示和隐藏
- 使用style样式隐藏显示下级菜单的方法
- 隐藏键盘和显示键盘
- Android中软键盘使用总结(一)
- 登录界面与Android软键盘显示、隐藏的交互设计