获取键盘通知的 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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

键盘的 UserInfo

keyboard 的通知的 UserInfo中包含的内容有如下:

UIKeyboardFrameBeginUserInfoKey //初始的 frame
UIKeyboardFrameEndUserInfoKey   //结束的 frame
UIKeyboardAnimationDurationUserInfoKey  //持续的时间
UIKeyboardAnimationCurveUserInfoKey  //UIViewAnimationCurve
UIKeyboardIsLocalUserInfoKey  //是否是当前 App的键盘

//已弃用
UIKeyboardCenterBeginUserInfoKey   NS_DEPRECATED_IOS
UIKeyboardCenterEndUserInfoKey     NS_DEPRECATED_IOS
UIKeyboardBoundsUserInfoKey        NS_DEPRECATED_IOS     
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

例子

- (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
{

}
 
 
  • 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}”; 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值