Http://www.cocoachina.com/ios/20150703/12365.html
In the interface that involves form input, we usually need to listen to some keyboard events and perform the corresponding actions according to the actual needs. For example, when the keyboard bounces, let our uiscrollview automatically shrink to see the contents of the entire uiscrollview. To do this, the following 6 notification constants are defined in UIWindow.h to match the keyboard's event handling at different points in time:
123456 |
uikeyboardwillshownotification // keyboard display before uikeyboarddidshownotification // keyboard display completed uikeyboardwillhidenotification // keyboard hidden before uikeyboarddidhidenotification // keyboard message after uikeyboardwillchangeframenotification // keyboard size changed before UIKEYBOARDDIDCHANGEFRAMENOTIFICATION     // keyboard size changed after |
The object objects for these notifications are nil. And the UserInfo dictionary contains some information about the keyboard, mainly the position size of the keyboard, we can use the following key to get the corresponding value in the dictionary:
12345678 |
// 键盘在动画开始前的frame let UIKeyboardFrameBeginUserInfoKey: String // 键盘在动画线束后的frame let UIKeyboardFrameEndUserInfoKey: String // 键盘的动画曲线 let UIKeyboardAnimationCurveUserInfoKey: String // 键盘的动画时间 let UIKeyboardAnimationDurationUserInfoKey: String |
Here, I am interested in the order in which keyboard events are called and how to get the size of the keyboard to adjust the size of the view appropriately.
As you can see from the type of keyboard notification defined, we are actually focusing on the events of the three-stage keyboard: Display, hide, size change. Here we set two Uitextfield, they have different keyboard types: one is a normal keyboard, and the other is a numeric keypad. We listen to all the keyboard events and print the relevant logs (no code is posted here) and look directly at the results.
1) When we let TextField1 get the input focus, the printed log is as follows:
1234 |
keyboard will change keyboard will show keyboard did change keyboard did show |
2) without hiding the keyboard, let TextField2 get the focus, print the following log:
1234 |
keyboard will change keyboard will show keyboard did change keyboard did show |
3) After the keyboard is closed, print the following log:
1234 |
keyboard will change keyboard will hide keyboard did change keyboard did hide |
As you can see from the above log, whether the keyboard is displayed or hidden, notifications of size changes are sent, and before the corresponding event for show and hide. When switching between keyboards of different sizes, the show event (not sending the Hide event) is sent in addition to the Change event.
There are two additional points to note:
If you are switching between two keyboards of the same size, no messages will be sent
If the switch is similar to the English-Chinese keyboard in the normal keyboard, as long as the size changes, will send a group or groups of 2 with the same process of the message
Knowing the order in which events are called, we can decide in which message processing method to perform the operation according to our own needs. To do this, we need to get some useful information. This information is encapsulated in the notification UserInfo, with the above constant key to get the relevant value. Usually we are concerned with Uikeyboardframeenduserinfokey, to get the animation completed, the keyboard frame, in order to calculate the height of our scroll view. In addition, we may want to change the scroll view height by animating the transition, At this time Uikeyboardanimationcurveuserinfokey and Uikeyboardanimationdurationuserinfokey are useful.
We can get these values in the following ways:
123456789 |
if
let dict = notification.userInfo {
var
animationDuration: NSTimeInterval = 0
var
animationCurve: UIViewAnimationCurve = .EaseInOut
var
keyboardEndFrame: CGRect = CGRectZero
dict[UIKeyboardAnimationCurveUserInfoKey]?.getValue(&animationCurve)
dict[UIKeyboardAnimationDurationUserInfoKey]?.getValue(&animationDuration)
dict[UIKeyboardFrameEndUserInfoKey]?.getValue(&keyboardEndFrame)
......
}
|
In fact, there are three additional values in UserInfo, except that these values have been deprecated since iOS 3.2. So we don't have to pay much attention.
The last one is the form. A form interface is simple to look at, but the interaction and UI always come up with ways to make it complicated, and in fact there are a lot of details in the design. Like our financial apps, it usually involves a lot of form input, so how to do it, or need to spend a few thoughts. When you are free, plan to summarize and write an article.
Reference
UIWindow Class Reference
Call order of keyboard events and get keyboard size