Call order of keyboard events and get keyboard size

Source: Internet
Author: User

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 // 键盘在动画开始前的framelet UIKeyboardFrameBeginUserInfoKey: String// 键盘在动画线束后的framelet 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 changekeyboard will showkeyboard did changekeyboard did show

2) without hiding the keyboard, let TextField2 get the focus, print the following log:

1234 keyboard will changekeyboard will showkeyboard did changekeyboard did show

3) After the keyboard is closed, print the following log:

1234 keyboard will changekeyboard will hidekeyboard did changekeyboard 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:

    1. If you are switching between two keyboards of the same size, no messages will be sent

    2. 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 iflet dict = notification.userInfo {    varanimationDuration: NSTimeInterval = 0    varanimationCurve: UIViewAnimationCurve = .EaseInOut    varkeyboardEndFrame: 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

    1. UIWindow Class Reference

Call order of keyboard events and get keyboard size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.