消息机制之通知中心传值(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
UIKeyboardFrameEndUserInfoKey
UIKeyboardAnimationDurationUserInfoKey
UIKeyboardAnimationCurveUserInfoKey
UIKeyboardIsLocalUserInfoKey
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];
NSValue *beginFrame = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardBeginRect = [beginFrame CGRectValue];
NSValue *endFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardEndRect = [endFrame CGRectValue];
CGFloat keyboardHeight = keyboardEndRect.size.height;
NSNumber *time = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *isCurrentKB = [userInfo objectForKey:UIKeyboardIsLocalUserInfoKey];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
打印结果
打印 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}”;
}
}