获取键盘通知的 UserInfo
消息机制之通知中心传值(NSNotificationCenter)中详细说了用户自定义的通知中心传值. 其实在系统中还有很多系统自带的通知, 系统来控制 post 时机, 如键盘的相关通知, UIWindow 的相关通知等等. 在这些通知中, 系统在 post 时, 自带了UserInfo, 今天来说一说键盘的通知和其 UserInfo.
例举关于 UIWindow和 Keyboard的通知:
UIWindowDidBecomeVisibleNotification; // nil
UIWindowDidBecomeHiddenNotification; // nil
UIWindowDidBecomeKeyNotification; // nil
UIWindowDidResignKeyNotification; // nil
UIKeyboardWillShowNotification; //键盘将要出现
UIKeyboardDidShowNotification; //键盘已经出现
UIKeyboardWillHideNotification; //键盘将要消失
UIKeyboardDidHideNotification; //键盘已经消失
UIKeyboardWillChangeFrameNotification; //键盘将要改变 frame
UIKeyboardDidChangeFrameNotification; //键盘已经改变 frame
键盘的 UserInfo
keyboard 的通知的 UserInfo中包含的内容有如下:
UIKeyboardFrameBeginUserInfoKey //初始的 frame
UIKeyboardFrameEndUserInfoKey //结束的 frame
UIKeyboardAnimationDurationUserInfoKey //持续的时间
UIKeyboardAnimationCurveUserInfoKey //UIViewAnimationCurve
UIKeyboardIsLocalUserInfoKey //是否是当前 App的键盘
//已弃用
UIKeyboardCenterBeginUserInfoKey NS_DEPRECATED_IOS
UIKeyboardCenterEndUserInfoKey NS_DEPRECATED_IOS
UIKeyboardBoundsUserInfoKey NS_DEPRECATED_IOS
例子
- (void)viewDidLoad {
[super viewDidLoad];
//监听,当键盘出现时自动发送消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//监听,当键退出时自动消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//键盘出现时接受消息通知
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo];
//获取键盘初始的frame
NSValue *beginFrame = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardBeginRect = [beginFrame CGRectValue];
//获取键盘结束的frame
NSValue *endFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardEndRect = [endFrame CGRectValue];
//获取当前键盘高度
CGFloat keyboardHeight = keyboardEndRect.size.height;
//获取持续的时间
NSNumber *time = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//是否是当前 app 的键盘
NSNumber *isCurrentKB = [userInfo objectForKey:UIKeyboardIsLocalUserInfoKey];
}
//键盘消失时接受消息通知
- (void)keyboardWillHide:(NSNotification *)aNotification
{
}
打印结果
打印 KeyBoard 的 notification, 后三个在 iOS3.2后已被弃用.
{
name = UIKeyboardWillShowNotification;
userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = “0.25”’
UIKeyboardFrameBeginUserInfoKey = “NSRect: {{0, 667}, {375, 258}}”;
UIKeyboardFrameEndUserInfoKey = “NSRect: {{0, 409}, {375, 258}}”;
UIKeyboardIsLocalUserInfoKey = 1;
UIKeyboardBoundsUserInfoKey = “NSRect: {{0, 0}, {375, 258}}”;
UIKeyboardCenterBeginUserInfoKey = “NSPoint: {187.5, 796}”;
UIKeyboardCenterEndUserInfoKey = “NSPoint: {187.5, 538}”;
}
}
下面是监听通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
监听方法
/**
* 键盘的frame发生改变时调用(显示、隐藏等)
*/
- (void)keyboardShow:(NSNotification *)notification
{
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect viewFrame = self.view.frame;
self.view.frame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, UIScreen.mainScreen.bounds.size.height - keyboardRect.size.height);
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardHide:(NSNotification*)notification {
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect viewFrame = self.view.frame;
self.view.frame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, UIScreen.mainScreen.bounds.size.height);
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}