2.键盘的显示/隐藏
2015-07-28 16:01
441 查看
应用中,跟用户交互功能必不可少!
关于键盘的显示/隐藏 中,就隐藏着许多的知识。那我们就来窥看一角。
我们常常抱怨这么几种情况:
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];
}
b985
与键盘的显示事件的回调函数相反。我收到了,[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];
}
b985
与键盘的显示事件的回调函数相反。我收到了,[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的好处,不用手动释放,保证内存不被泄漏。
希望对你有所帮助!
相关文章推荐
- iOS开发之路--微博“更多”页面
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- IOS开发代码分享之设置UISearchBar的背景颜色
- ios开发之环境搭建-01
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- 学习Objective-C:入门手册
- iOS开发 简单实现视频音频的边下边播
- iOS开发 — Quartz 2D知识点应用 (制作了一个Demo,源代码)
- ios开发对配置文件plist的操作
- iphone5和4的屏幕兼容问题
- 9秒的课程只为培养靠谱的IOS和HTML5开发人才而定制
- iOS开发学习笔记 1-0 开篇
- iOS开发学习笔记 2-1 C语言部分 初识C语言
- iOS开发学习笔记 2-2 C语言部分 类型、运算符与表达式