简体   繁体   中英

Why is -animateWithDuration:delay:options:animations:completion: blocking the UI?

I always thought that Core Animation performs animations on the background. When I run this code, my UI interactions are blocked until the animation finishes:

[UIView animateWithDuration:4.5 
                      delay:0 
                    options:options 
                 animations:^{
                    oldView.alpha = 0;
                    newView.alpha = 1;
                 }  
                 completion:^(BOOL finished) {
                    if (finished) {
                        [oldView removeFromSuperview];
                    }
                 }];

Like you can see the duration is long so it's clearly visible that the UI interactions are blocked while animating.

The UI interaction begins to be blocked when the animation begins, and ends to be blocked when the animation finishes.

Is there a way to start a UIView animation concurrently so the UI interactions are not blocked?

EDIT to clarify: I know that the UI is blocked because I cannot interact with any control on the screen why this animation is running. Those other controls are not related with what is being animated and are not on the same branch in the view hierarchy. The entire UI is blocked when this animation runs. When I set it to 10 seconds, the UI is blocked for 10 seconds while the animation runs. Tested with iOS 4.2 on iPhone 4.

Looking at the documentation for UIView, I found this in the discussion section for that method:

During an animation, user interactions are temporarily disabled for the views being animated. (Prior to iOS 5, user interactions are disabled for the entire application.) If you want users to be able to interact with the views, include the UIViewAnimationOptionAllowUserInteraction constant in the options parameter.

So, if you want user interaction to continue to be allowed, you must set this constant in the options parameter.

Documentation is your friend :)

UIView Class Reference

Under animateWithDuration:animations:

"During an animation, user interactions are temporarily disabled for the views being animated. (Prior to iOS 5, user interactions are disabled for the entire application.)"

And of course, for all views under the animated view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM