The problem that occurs when you execute the animateWithDuration: delay: options: animations: completion of the UIView in a row,

Source: Internet
Author: User

The problem that occurs when you execute the animateWithDuration: delay: options: animations: completion of the UIView in a row,

  • Problem background
The basic principle of custom accessoryView for UIKeyboard is to dynamically adjust the frame value of accessoryView Based on the frame of UIKeyboard. To achieve smooth changes to accessoryView, we use the class method of UIView to achieve animateWithDuration: delay: options: animations: completion: to dynamically adjust the frame value of accessoryView
  • Problem description
We trigger the call to animateWithDuration: delay: options: animations: completion: Based on the notification UIKeyboardWillShowNotification. The problem arises, when the property autocorrectionType of UITextField is set to UITextAutocorrectionTypeYes (that is, the keyboard is automatically switched to the Chinese Keyboard when the Chinese system invokes the keyboard), and when the keyboard is opened for the first time, the system will send multiple UIKeyboardWillShowNotification notifications, which will trigger multiple calls to animateWithDuration: delay: options: animations: completion:. Let's first look at the code: this code will be executed continuously multiple times, according to self. the change of frame (self corresponds to accessoryView) only needs to be processed in the block of animations. However, the original author of the Code does not know why it is also executed in competion, let's take a look at the content in NSLog: Pay attention to the y-axis value of the frame of each selfEndingRect. We expect the final value to be 276, but we can see that the y-axis value of the frame of selfEndingRect in the completion is in the reverse, the final value is 312, that is, the value at the first call. As a result, the final display position of accessoryView is 36 lower than expected.
  • Problem Analysis
Some useless analysis processes will not be repeated. Just look at the final result analysis. First, write a piece of test code and perform multiple animate operations cyclically, the execution time of each animation is 10 seconds. Let's take a look at the log: it is not difficult to find that the completion of the 0th animation operation is finally executed, that is, after the animation execution time of 10 seconds, the last four times of animate were directly cancel. From the time point, we can also find that the start time of animate and the end time of completion are about 47 seconds, and the difference between the two is 0. * The second time is much smaller than the set animation time of 10 seconds. The strange thing here is that since it is cancel, why is the value of finish still YES?
  • Solution
In this example, we can remove the assign value to the frame in completion. In general, the animation operation will not be executed in the same loop as in this example. If we want to implement continuous animation, the simple method is to continue executing animate in the completion to ensure that the next animation is executed on the basis of the previous completion.

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.