[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];
- (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];
}
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}}";