iOS:我应该为动画添加 UIViews 或 CALayers 吗?

发布于 2024-10-11 15:17:31 字数 141 浏览 8 评论 0原文

假设我想将 50 张图像添加到视图中以使其具有动画效果。假设我计划使用核心动画(例如 CABasicAnimation)而不是“UIView”动画。

我是否最好通过添加 50 个子视图或 50 个子层来实现此目的?有什么区别吗?

谢谢。

Let's say I want to add 50 images to a view for the purpose of animating them. And let's suppose I'm planning on using Core Animation (e.g., CABasicAnimation) rather than "UIView" animation.

Am I better off implementing this by adding 50 subviews or 50 sublayers? Does it make a difference?

Thanks.

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

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

发布评论

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

评论(4

安人多梦 2024-10-18 15:17:31

正如我此处所描述的,我在动画中同时使用 UIViews 和 CALayers,发现它们之间的性能差异可以忽略不计。 UIViews 是层的非常轻量级的包装器。此外,您需要的任何基于图层的动画都可以轻松应用于 UIView 的支持图层。

我在想要创建跨平台(Mac / iOS)UI 元素的情况下直接使用了 CALayers,因为 CALayers 在两个平台上的实现几乎相同(与显着不同的 NSView 和 UIView 不同)。 CALayers 没有任何开箱即用的触摸处理例程,但如果需要,您可以添加该功能。

还有一些边缘情况,您可能希望直接使用图层,例如尝试对图层进行有限的 3D 操作(如在 CoverFlow 效果中)或使用 CAReplicatorLayer 产生粒子效果时。

As I describe here, I've used both UIViews and CALayers in animations and found a negligible performance difference between them. UIViews are very lightweight wrappers around the layers. Also, any layer-based animations you need can be applied to a UIView's backing layer easily.

I've used CALayers directly in situations where I wanted to create cross-platform (Mac / iOS) UI elements, because CALayers are almost identical in their implementation on both platforms (unlike the significantly different NSViews and UIViews). CALayers don't have any touch-handling routines out of the box, but you can add that capability if you need to.

There are also some edge cases where you might want to work directly with layers, like when trying to do limited 3-D manipulation of the layers (as in a CoverFlow effect) or when using a CAReplicatorLayer to produce particle effects.

樱桃奶球 2024-10-18 15:17:31

UIView 包含子层,因此它们更重,并且包含所有 50 个图像可能不需要的内容,例如事件和触摸处理程序/变量。因此,与对每个图像使用视图相比,使用图层可能会稍微更高效并且使用更少的内存。

UIViews contain sublayers, so they are heavier weight, and contain stuff you probably don't need for all 50 images, such as event and touch handlers/variables. So using layers would probably be slightly more efficient and use a bit less memory than using views for each image.

靖瑶 2024-10-18 15:17:31

如此少量图像的差异可以忽略不计。使用最方便的。

The difference for such a small number of images is negligible. Use what's most convenient.

仅此而已 2024-10-18 15:17:31

我还没有做过动画(还:-),但我记得读过的有关它的内容建议创建一张图像,将所有 50 个平铺在其上,然后在绘制时偏移到正确的图像。这样你只需要一层或 UIImage 或其他任何东西来显示它。我不知道速度,但我猜它会节省内存,并且可能更容易管理和编码。

I've not done animation (yet :-), but the stuff I remember reading about it suggests to create one image with all 50 tiled on it and then just offset to the correct image when drawing. That way you only need one layer or UIImage or whatever to display it. I don't know about speed, but I'd guess it would save memory and would probably be easier to manage and code.

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