//通过设置该监听,用于获取键盘弹出时的轨迹属性及键盘frame
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//通过设置该监听,用于获取键盘关闭时的轨迹属性及键盘frame
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
#pragma mark - 键盘事件监听
- (void)keyboardWillShow:(NSNotification *)notice {
UIViewAnimationCurve _animationCurve;
CGFloat _animationDuration;
NSDictionary *userInfo = [notice userInfo];
CGRect endFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat _keyboardHeight = (endFrame.origin.y != kCCScreenHeight()) ? endFrame.size.height:0;
if (!_keyboardHeight) return;
CGRect beginRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
if(!(beginRect.size.height > 0 && ( fabs(beginRect.origin.y - endRect.origin.y) > 0))) return;
//动画执行时长
_animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//运行轨迹属性
_animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
//这样就可以与键盘动画无缝衔接了
[UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:^{
// 修改frame
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)noti {
//获取键盘的高度
NSDictionary *userInfo = [noti userInfo];
// NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration delay:0 options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:^{
// 修改frame
} completion:nil];
}
网友评论