应用中,跟用户交互功能必不可少!
关于键盘的显示/隐藏 中,就隐藏着许多的知识。那我们就来窥看一角。
我们常常抱怨这么几种情况:
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的好处,不用手动释放,保证内存不被泄漏。
希望对你有所帮助!