ios SDK开发之键盘通知
分类:日常开发问题Wiki|作者: eric_xjj相关|发布日期 : 2015-01-23|热度 : 477°
前几日发现一个iOS键盘通知方面的小bug,因此总结下iOS键盘通知的知识点。在iOS中,键盘通知目前的SDK里总共有如下6个:
UIKeyboardWillShowNotification: 显示键盘的时候立即发出该通知
UIKeyboardDidShowNotification:显示键盘后才发出该通知
UIKeyboardWillHideNotification:键盘即将消失的时候立即发出该通知
UIKeyboardDidHideNotification:键盘消失后才发出该通知
UIKeyboardWillChangeFrameNotification:键盘的frame值发生变化的时候立即发出该通知
UIKeyboardDidChangeFrameNotification:键盘的frame值发生变化后才发出该通知
其中,以上通知的userInfo字典中包含了一些键盘frame以及动画相关的信息,字典的key如下:
NSString *const UIKeyboardFrameBeginUserInfoKey;//userInfo字典里该key对应一个NSValue,存储一个包含键盘初始frame值的CGRect结构(即键盘刚出现时的frame值)
NSString *const UIKeyboardFrameEndUserInfoKey;//userInfo字典里该key对应一个NSValue,存储一个包含键盘结束frame值的CGRect结构(即键盘动画结束后的frame值)
NSString *const UIKeyboardAnimationDurationUserInfoKey;//userInfo字典里该key对应一个NSNumber,存储一个包含键盘进入或离开屏幕的UIViewAnimationCurve结构
NSString *const UIKeyboardAnimationCurveUserInfoKey;//userInfo字典里该key对应一个NSNumber,存储一个包含键盘动画时间的double值,时间以秒为单位。
为了加深理解,下面分别打印出一个键盘显示、一个键盘消失以及两个屏幕旋转期间(这个在下面将会介绍)发出的键盘通知结构(模拟器打印的,真机诸位同学可以自己打印观察):
01{name = UIKeyboardDidShowNotification; userInfo = { //键盘显示时发出
02UIKeyboardAnimationCurveUserInfoKey = 0;
03UIKeyboardAnimationDurationUserInfoKey = "0.25";
04UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}";
05UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 401}";
06UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 239}";
07UIKeyboardFrameBeginUserInfoKey = "NSRect: {{-162, 0}, {162, 480}}";
08UIKeyboardFrameChangedByUserInteraction = 0;
09UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 0}, {162, 480}}";
10}}
11
12{name = UIKeyboardDidHideNotification; userInfo = {//键盘消失时发出
13UIKeyboardAnimationCurveUserInfoKey = 0;
14UIKeyboardAnimationDurationUserInfoKey = "0.25";
15UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}";
16UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 239}";
17UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 401}";
18UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 0}, {162, 480}}";
19UIKeyboardFrameChangedByUserInteraction = 0;
20UIKeyboardFrameEndUserInfoKey = "NSRect: {{-162, 0}, {162, 480}}";
21}}
22
23{name = UIKeyboardWillChangeFrameNotification; userInfo = {//屏幕旋转期间并且键盘存在的时候发出
24UIKeyboardAnimationCurveUserInfoKey = 0;
25UIKeyboardAnimationDurationUserInfoKey = "0.25";
26UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}";
27UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 401}";
28UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 239}";
29UIKeyboardFrameBeginUserInfoKey = "NSRect: {{158, 0}, {162, 480}}";
30UIKeyboardFrameChangedByUserInteraction = 0;
31UIKeyboardFrameEndUserInfoKey = "NSRect: {{158, 0}, {162, 480}}";
32}}
33
34{name = UIKeyboardDidChangeFrameNotification; userInfo = {//屏幕旋转期间并且键盘存在的时候发出
35UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}";
36UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 401}";
37UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 239}";
38UIKeyboardFrameBeginUserInfoKey = "NSRect: {{320, 0}, {162, 480}}";
39UIKeyboardFrameChangedByUserInteraction = 0;
40UIKeyboardFrameEndUserInfoKey = "NSRect: {{158, 0}, {162, 480}}";
41}}
下面本文着重讲解关于键盘通知的发出顺序,当一个UITextView或UITextField变成第一焦点时,通知的发出顺序如下:UIKeyboardWillChangeFrameNotification、UIKeyboardWillShowNotification、UIKeyboardDidChangeFrameNotification、UIKeyboardDidShowNotification。
当一个UITextView或UITextField注销焦点状态时,发出通知顺序如下:UIKeyboardWillChangeFrameNotification、UIKeyboardWillHideNotification、UIKeyboardDidChangeFrameNotification、UIKeyboardDidHideNotification。
特别需要注意的是,当屏幕旋转的时候也会发出键盘通知,并且顺序如下:UIKeyboardWillChangeFrameNotification、UIKeyboardWillHideNotification、UIKeyboardDidChangeFrameNotification、UIKeyboardDidHideNotification、UIKeyboardWillChangeFrameNotification、UIKeyboardWillShowNotification、UIKeyboardDidChangeFrameNotification、UIKeyboardDidShowNotification。细心的读者肯定发现了,其顺序是一个UITextView或UITextField注销焦点状态时,发出键盘通知顺序和一个UITextView或UITextField变成第一焦点时发出键盘顺序组合起来一致,也就是说,在屏幕旋转期间,虽然我们看起来好像键盘没啥变化,但实际上通知已经经理了从“消失”到再次“显示”的路径了。所以,平时如果需要捕获键盘通知处理一些业务逻辑的话,需要特别注意通知的顺序,小心谨慎,避免意外的错误。笔者就是在这个地方犯了一个比较严重的错误,检查半天才找出来,希望各位童鞋引以为戒,比如:如果你希望在键盘弹出的时候捕获UIKeyboardDidShowNotification,以便添加一个透明的全屏的按钮(点击该按钮的时候让键盘消失);然后捕获UIKeyboardDidHideNotification进行了一个动画,在该动画结束后移除上述全屏按钮;这样当屏幕旋转的时候,你就会发现,键盘明明没消失,但是上述全屏按钮已经不翼而飞(请聪明的读者朋友自己想想,这是为什么?)。
网友评论