常见的键盘通知
UIKeyboardWillShowNotification 显示键盘的时候立即发出该通知
UIKeyboardDidShowNotification 显示键盘后才发出该通知
UIKeyboardWillHideNotification 键盘即将消失的时候立即发出该通知
UIKeyboardDidHideNotification 键盘消失后才发出该通知
UIKeyboardWillChangeFrameNotification 键盘的frame值发生变化的时候立即发出该通知
UIKeyboardDidChangeFrameNotification 键盘的frame值发生变化后才发出该通知
键盘动画的信息
notification.userInfo
{
UIKeyboardAnimationCurveUserInfoKey = 7;//动画曲线类型
UIKeyboardAnimationDurationUserInfoKey = "0.25";//动画持续时间
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 226}}";//键盘的bounds
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 849}";//键盘动画起始时的中心点
UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 623}";//键盘动画结束时的中心点
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 226}}";//键盘动画起始时的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 510}, {414, 226}}";//键盘动画结束时的frame
UIKeyboardIsLocalUserInfoKey = 1;//键盘是否显示,bool类型,1 show,2 hide
}
由于iPad有拆分键盘的功能,所以只能通过ChangeFrameNotification通知去判断键盘的显示与隐藏。
按照项目要求,还需加上一个输入条随着键盘的高度变化而变化。
目前做的不完美,还在完善:
//添加键盘相关的通知
- (void)handleKeyboardNotf
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//删除键盘相关的通知
- (void)removeKeyboardNotf
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)handleKeyboardillChangeFrame:(NSNotification *)notification {
CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardBeginY = keyboardBeginFrame.origin.y;
CGFloat keyboardEndY = keyboardEndFrame.origin.y;
if(fabs(CGRectGetHeight(keyboardBeginFrame)-CGRectGetHeight(keyboardEndFrame))==55 || CGRectGetHeight(keyboardBeginFrame) == CGRectGetHeight(keyboardEndFrame))
{
BOOL isShow = (keyboardBeginY-keyboardEndY)>0;
if (isShow) {//显示
self.inputView.hidden = NO;
self.maskView.hidden = NO;
float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:animationDuration animations:^{
self.inputView.frame = CGRectMake(0, keyboardEndY-InputViewH, SCREEN_WIDTH, InputViewH);
}];
}else{//隐藏
float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:animationDuration animations:^{
self.inputView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, InputViewH);
} completion:^(BOOL finished) {
if (keyboardEndY>=SCREEN_HEIGHT) {
[self hidSendView];
}
}];
}
}else{//键盘切换状态
float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:animationDuration animations:^{
self.inputView.hidden = NO;
self.maskView.hidden = NO;
if (keyboardEndY==0) {
self.inputView.frame = CGRectMake(0, STATUSBAR_HEIGHT, SCREEN_WIDTH, InputViewH);
}else{
self.inputView.frame = CGRectMake(0, keyboardEndY-InputViewH, SCREEN_WIDTH, InputViewH);
}
} completion:^(BOOL finished) {
}];
}
}