何时以及何时不使用 CoreAnimation
在为 iPhone 编程时,什么时候最好使用 CoreAnimation 而不是其他 API(反之亦然)? OpenGL 具有更通用的功能(尽可能低级)。 CoreAnimation 的设置要容易得多,并且具有缓存等功能。
我正在制作一个游戏(2D),它涉及很多物理知识。我真的希望这款游戏能够以至少 50 FPS 的速度运行,最好是 60(或者任何跟上垂直同步所需的速度)。我不知道在这种情况下如何使用动画,但我可以将所有内容分成精灵/CALayers。我将在每一帧设置它们的位置和旋转(也许只是可见的帧)。许多精灵都是相同的。其中一些精灵每帧都会改变颜色。我不会使用很多图像,但我会使用很多渐变。
那么,CoreAnimation 会是优化还是瓶颈呢?什么时候最好使用CoreAnimation,什么时候最好使用其他API?
When programming for the iPhone, when is it best to use CoreAnimation over another API (and vice-versa)? OpenGL has a more generic range of features (being as low-level as it is). CoreAnimation is a lot easier to get set up, and it has features like caching.
I'm making a game (2D), and it involves a lot of physics. I really want this game to run at a minimum of 50 FPS, preferably 60 (or whatever is necessary to keep up with vsync). I don't know how to utilize animations in this case, but I can split everything up into sprites/CALayers. I'll be setting their position and rotation every frame (maybe just the visible ones). Many of the sprites are identical. Some of these sprites will be changing colors every frame. I won't be using many images, but I will be using a lot of gradients.
So, will CoreAnimation be an optimization or a bottleneck? When is it best to use CoreAnimation, and when is it best to use other APIs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
核心动画可以让您用最少的代码做很多事情。它使得管理二维图层及其动画相对容易。因为动画是为您处理的,即使是涉及复杂路径或时序曲线的动画,您也无需编写代码来更新每一帧的显示或计算动画的补间。 Core Animation 还与 Quartz 中的 2-D 绘图功能、本机 Cocoa Touch 界面元素和触摸处理以及应用程序中其他位置的 Objective-C 代码完美集成。
如果您按预期使用该框架,您可以获得非常好的性能。在旧的第一代 iPhone 上进行的基准测试中,我能够以大约 60 FPS 的速度在屏幕上同时制作 50 个半透明层的动画。然而,这是提前为每个层指定预定义的线性路径,并且不会中断动画。
OpenGL ES 适用于当您认为不直接与底层硬件对话就无法获得必要的性能时,或者当您需要执行真正的 3-D 工作时(Core Animation 可以执行有限的 3-D)效果,但仅限于矩形平面)。获得更好的性能和更大的灵活性的代价是需要编写更多难以阅读的代码。此外,有时调试 OpenGL ES 可能是一场噩梦。
OpenGL ES 的另一个潜在优势是它非常独立于平台,因此您可以以一种可以轻松从 iPhone 移植到 Android、桌面等的方式构建渲染器。
总的来说,我的建议首先查看 Core Animation,看看它是否可以实现您想要的功能,因为通过这种方式您可以更快地构建应用程序。如果这不合适,并且您正在考虑制作 2D 游戏,也许 cocos2d 会成为一条好路。我自己没有使用过该框架,但我听说它简化了执行 2-D OpenGL ES 工作的过程。仅当这些方法都不合适时,您才应该转向 OpenGL ES。
如果您好奇,我会在 我在 iTunes U 上的课程,您可以在其中浏览并了解在任一环境中工作所需的内容。
Core Animation lets you do a lot with a minimal amount of code. It makes managing 2-D layers and their animation relatively easy. Because animations are handled for you, even those involving complex paths or timing curves, you don't need to write code to update the display every frame or calculate the tweening of an animation. Core Animation also integrates nicely with the 2-D drawing capabilities you have in Quartz, with native Cocoa Touch interface elements and touch handling, and with your Objective-C code elsewhere in your application.
If you use the framework as intended, you can achieve very good performance. In my benchmarks on my old first-generation iPhone, I was able to animate 50 translucent layers at once onscreen at about 60 FPS. However, this is with predefined linear paths being specified for each layer ahead of time and no interruption of the animations.
OpenGL ES is for when you just don't think you'll be able to get the necessary performance without talking directly to the underlying hardware, or for when you need to do true 3-D work (Core Animation can do limited 3-D effects, but only with rectangular planes). The tradeoff for having better performance and more flexibility is the need to write a lot more code that is a lot harder to read. Also, it can be a nightmare to debug OpenGL ES sometimes.
One other potential advantage of OpenGL ES is that it is pretty platform-independent, so you might be able to build your renderer in such a way that it can easily be transplanted from iPhone to Android, the desktop, etc.
In general, my recommendation has been to look to Core Animation first to see if it can do what you want, because you'll be able to build an application much faster this way. If that's not appropriate, and you're looking at doing a 2-D game, perhaps cocos2d would be a good path. I've not used the framework myself, but I've heard that it simplifies the process of doing 2-D OpenGL ES work. Only if neither of these approaches are appropriate should you drop down to OpenGL ES.
If you're curious, I do cover both Core Animation and OpenGL ES in the videos for my course on iTunes U, where you can skim through and see what's needed to work in either environment.