对 CALayer 属性动画并发数量的合理预期?

发布于 2024-09-09 08:10:25 字数 808 浏览 3 评论 0原文

我使用 UIView 动画块在显示屏上的多个图层上同时对 CALayer 属性(本例中为背景颜色)进行动画处理。

所有图层都是不透明的,我在一个块中对所有内容进行动画处理,基本上就像这样

[UIView beginAnimations:@"outer" context:nil];
float duration = .25;
float offset = 0.0;
for( NSArray *viewsInPass in viewQueue ) {
   for( UIView *innerView in viewInPass ) {
       [UIView beginAnimations:@"inner" context:nil];
       [UIView setAnimationDelay:offset];
       [UIView setAnimationDuration:duration];
       [innerView.layer setBackgroundColor:[newColor CGColog]];
       [UIView commitAnimations];
   }
   offset += duration;
}
[UIView commitAnimations];

一旦有 4-5 个并发图层对其背景颜色进行动画处理,它就会变得非常不稳定,设备基本上开始完全失去其渲染速率,然后冻结到最后剩余的动画。所有视图都不重叠,并且都是不透明的,通常约为 20x20 像素。

我对它的性能如此糟糕感到有点震惊,特别是在阅读了这么多关于 Quartz 2D 等令人欣喜的事情之后。我觉得我一定在这里错过了一些基本的东西!

帮助!

I'm using UIView animation blocks to animate CALayer properties (backgroundColor in this case) on multiple layers on the display at once.

All layers are opaque, and I'm animating everything in one block, essentially like this

[UIView beginAnimations:@"outer" context:nil];
float duration = .25;
float offset = 0.0;
for( NSArray *viewsInPass in viewQueue ) {
   for( UIView *innerView in viewInPass ) {
       [UIView beginAnimations:@"inner" context:nil];
       [UIView setAnimationDelay:offset];
       [UIView setAnimationDuration:duration];
       [innerView.layer setBackgroundColor:[newColor CGColog]];
       [UIView commitAnimations];
   }
   offset += duration;
}
[UIView commitAnimations];

Once this has 4-5 concurrent layers animating their background color it get very choppy and the device essentially starts missing its render rate entirely and just freezes to the end of the remaining animations. All views do not overlap and they are all opaque, and are generally about 20x20 pixels.

I was a little shocked at how non-performant this is, especially after reading so many gratifying things about Quartz 2D etc. I feel like I must be missing something fundamental here!

Help!

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

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

发布评论

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

评论(3

玉环 2024-09-16 08:10:25

视图中的不透明性是最大的性能影响 - 如果您可以在整个渲染过程中防止它们重叠和完全不透明(即视图序列中的任何地方都没有透明度,包括背景),您将获得稍微更好的性能。

我设法以约 30 fps(即整个屏幕)的速度获得单个大型动画,但遇到内存限制(原始 iPhone/iPod),最终导致我无法正常工作。如果您想要的不仅仅是您所获得的内容,您需要进入 OpenGL 领域 - 通过游戏框架或直接进入。在该循环中进行 Objective-C 调用以在循环浏览图像的同时更改偏移量的开销最终会杀死您。

Opaqueness in the views is the single biggest performance impact - if you can keep them from overlapping and entirely opaque through the whole rendering process (i.e. no transparency anywhere in that view sequence, including the background), you'll get slightly better perf.

I managed to get a single large animation up at ~30 fps (i.e. whole screen), but ran into memory limitations (original iPhone/iPod) that ultimately killed me beyond that. If you want more than about what you're getting there, you'll need go into OpenGL land - either through a gaming framework or directly. The overhead of the objective-C calls in that loop to enable changing the offset at the same time that you're cycling through images will ultimately kill you.

不美如何 2024-09-16 08:10:25

编辑:[已删除]

由于每个动画都在前一个动画结束时开始,因此您可以在 NSTimer 或委托 didFinish 回调中启动每个动画,而不是立即对所有动画进行排队。这将对动画系统提出更少的要求。

您应该尝试确保没有视图同时以不同的颜色排队。

Edit: [removed]

Since you have each animation start as the previous one ends, you could start each in an NSTimer or the delegate didFinish callback instead of queueing all the animations at once. That would put less demand on the animation system.

You should try to make sure that no views are queued with different colors at the same time.

杯别 2024-09-16 08:10:25

事实证明,问题在于您只需要在开始的偏移量之间添加某种 epsilon 即可。你最终会在每次传递的尾部和下一次传递的开始处得到 n*2 的动画,只是一瞬间。由于某种原因,这会使动画系统呕吐,即使它只是他们共享的处理切片。

使其成为

偏移量+=持续时间*1.05;

解决了堵塞动画的通道之间的抖动问题。

It turns out the issue is that all you need to add some sort of epsilon between the offsets you start with. You end up with n*2 animating at the tail of each pass and the start of the next just for an instant. For some reason this makes the animation system barf even if it's just a processing slice they share.

Making it be

offset += duration * 1.05;

resolved the jerkiness between passes that clogged up the animations.

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