首先注册通知,监听键盘的变动:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
[nc addObserverForName:UIKeyboardWillChangeFrameNotification
object:nil
queue:mainQuene
usingBlock:^(NSNotification *note){
[weakSelf keyboardWillChangeFrame:note];
}];
[nc addObserverForName:UIKeyboardWillShowNotification
object:nil
queue:mainQuene
usingBlock:^(NSNotification *note){
[weakSelf keyboardWillShow:note];
}];
[nc addObserverForName:UIKeyboardWillHideNotification
object:nil
queue:mainQuene
usingBlock:^(NSNotification *note){
[weakSelf keyboardWillHide:note];
}];
键盘的frame和动画属性通知里都有,直接获取:
- (void)layoutView:(NSNotification *)noti {
//获取键盘高度
NSDictionary *userInfo = notification.userInfo;
CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
_keyboardHeight = _isVisiable? endFrame.size.height: 0;
//获取键盘动画属性
UIViewAnimationOptions animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGFloat animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:animationDuration
delay:0.0
options:animationCurve
animations:^{
//改变界面布局
} completion:nil];
}
网友评论