为什么-animateWithDuration:delay:options:animations:completion:阻止UI? [英] Why is -animateWithDuration:delay:options:animations:completion: blocking the UI?

查看:86
本文介绍了为什么-animateWithDuration:delay:options:animations:completion:阻止UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为Core Animation会在后台执行动画。当我运行此代码时,我的UI交互将被阻止,直到动画结束:

  [UIView animateWithDuration:4.5 
delay :0
选项:选项
动画:^ {
oldView.alpha = 0;
newView.alpha = 1;
}
完成:^(BOOL完成){
if(finished){
[oldView removeFromSuperview];
}
}];

您可以看到持续时间很长,因此可以清楚地看到UI动画在动画时被阻止。



动画开始时UI交互开始被阻止,动画结束时结束被阻止。



<有没有办法同时启动UIView动画,以便不阻止UI交互?



编辑澄清:我知道UI被阻止,因为我无法与屏幕上的任何控件进行交互,为什么这个动画正在运行。那些其他控件与动画内容无关,并且不在视图层次结构中的同一分支上。此动画运行时,将阻止整个UI。当我将其设置为10秒时,UI会在动画运行时被阻止10秒。在iPhone 4上使用iOS 4.2进行测试。

解决方案

查看UIView的文档,我在讨论部分找到了该方法:


在动画期间,对于正在设置动画的视图,会暂时禁用用户交互。 (在iOS 5之前,对整个应用程序禁用用户交互。)如果希望用户能够与视图交互,请在options参数中包含UIViewAnimationOptionAllowUserInteraction常量。


因此,如果您希望继续允许用户交互,则必须在options参数中设置此常量。


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.

这篇关于为什么-animateWithDuration:delay:options:animations:completion:阻止UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆