UIView 动画占用 CPU 时间

发布于 2024-11-04 07:55:56 字数 775 浏览 4 评论 0原文

我想知道如何在我的视图上拥有连续和重复的动画而不冻结我的应用程序的其余部分 - 因为我当前的方法导致 100% 动画,可能的 0% 用户交互 - 这有点..不好。

动画总体上很简单:我想让 uilabel 像警报器一样,将 alpha 属性从 1 更改为 0,然后再更改回来,依此类推。

因此,基本的动画块是:

    [UIView animateWithDuration:0.4f
                          delay:0.0f
                        options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                     animations:^(void){warningMessage.alpha = 0;}
                     completion:NULL];    

每次我失去对应用程序的控制时,我尝试在后台、后台线程中甚至在异步 GCD 块中调用它 - 但动画效果很好。 (好吧,苹果文档说它必须发生在主线程上,但我尝试了..)

现在问题又来了:如何同时拥有这两者?这样可能吗?

提前致谢!

PS:抱歉如果这是一次转发,在这里没有找到合适的解决方案/问题!

PPS:是的,我知道警报器不会改变它们的 alpha 属性:P

i wonder how i can have continuos and repeating animations on my Views w/o freezing the rest of my app - since my current approaches lead into 100% animation, 0% user interaction possible - which is kind of .. bad.

the animation in general is simple: i want to make an uilabel act like a siren, altering the alpha-property from 1 to 0 and back and so forth.

the basic animation block therefore is:

    [UIView animateWithDuration:0.4f
                          delay:0.0f
                        options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                     animations:^(void){warningMessage.alpha = 0;}
                     completion:NULL];    

i tried to call it in background, in backgroundthreads, even in an async GCD block, each time i loose control of the app - but the animation works fine.
(ok, apple docs say it MUST happen on main thread, but i tried..)

Now again, the question: how to have both of it? is it even possible in that way?

Thanks in advance!

P.S.: Sry if this is quite a repost, did not find a proper solution/question here!

P.P.S: yes, i know that sirens dont alter their alpha propery :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2024-11-11 07:55:56

您是否尝试将 UIViewAnimationOptionAllowUserInteraction 添加到选项标志中?

did you try adding the UIViewAnimationOptionAllowUserInteraction to the options flag?

云醉月微眠 2024-11-11 07:55:56

嗯.. 使用 uikit 制作动画肯定很容易.. 但我不会说它非常高效。您只需为 Alpha 制作动画,但背景中还会发生更多事情。 :)

动画已经在不同的线程中运行..因此在其他线程上启动它不会对您有帮助。

作为解决方案,我将在您的应用程序中注册一个 CADisplayLink 并在其更新函数中更改视图的 alpha ...如果您一直运行它,这将更加高效。

Well.. doing animations with uikit is for sure easy .. but I would not call it very efficient. You just animate the alpha, but there is much more happening in the background. :)

Animations already run in a different thread.. so starting it on an other thread won't help you.

As a solution I would register a CADisplayLink in your application and change the alpha of the view in it's update function ... that will be much more efficient if you run it all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文