前几日发现一个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值,时间以秒为单位。
为了加深理解,下面分别打印出一个键盘显示、一个键盘消失以及两个屏幕旋转期间(这个在下面将会介绍)发出的键盘通知结构(模拟器打印的,真机诸位同学可以自己打印观察):
- {name = UIKeyboardDidShowNotification; userInfo = { //键盘显示时发出 UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 401}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 239}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{-162, 0}, {162, 480}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 0}, {162, 480}}";}}{name = UIKeyboardDidHideNotification; userInfo = {//键盘消失时发出 UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 239}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 401}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 0}, {162, 480}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{-162, 0}, {162, 480}}";}}{name = UIKeyboardWillChangeFrameNotification; userInfo = {//屏幕旋转期间并且键盘存在的时候发出 UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 401}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 239}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{158, 0}, {162, 480}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{158, 0}, {162, 480}}";}}{name = UIKeyboardDidChangeFrameNotification; userInfo = {//屏幕旋转期间并且键盘存在的时候发出 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {480, 162}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {240, 401}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {240, 239}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{320, 0}, {162, 480}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{158, 0}, {162, 480}}";}}
下面本文着重讲解关于键盘通知的发出顺序,当一个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进行了一个动画,在该动画结束后移除上述全屏按钮;这样当屏幕旋转的时候,你就会发现,键盘明明没消失,但是上述全屏按钮已经不翼而飞(请聪明的读者朋友自己想想,这是为什么?)。