优化Core Graphics动画绘图(iPhone)

发布于 2024-09-11 03:57:32 字数 265 浏览 10 评论 0原文

我有一个每秒触发函数 30 次的循环。该函数改变了我用来制作动画的几个点的位置。我通过所有点绘制线条,这意味着线条每秒会改变 30 次。

我将这些线绘制到 CGLayer,然后在 drawRect: 方法中将其绘制到 UIView。我这样做是因为我知道在屏幕外绘制时性能会得到提高。然而,似乎 CGLayer 保存了所有实际的线条而不是绘制的像素,因为即使我清除它,当绘制更多的线条时,程序也会随着时间的推移运行得越来越慢。

我正在寻求其他方法来制作动画。最有效的方法是什么?为什么?

I have a loop that fires a function 30 times per second. The function changes the position of a couple of points that I use to animate. I draw lines through all the points, meaning that the lines will change 30 times per second.

I draw these lines to a CGLayer, which then is drawn to a UIView in the drawRect: method. I do this because I understand that performance is improved when drawing offscreen. However, it seems that the CGLayer saves all actual lines instead of drawn pixels, since even if I clear it, the program runs slower and slower over time when more lines are drawn.

I'm asking for other ways to animate this. What's the most efficient way and why?

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

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

发布评论

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

评论(2

忱杏 2024-09-18 03:57:32

您应该使用核心动画框架来为图层上的属性设置动画。您可以子类化 CALayer 或使用委托来进行实际绘图。在绘图代码中,您可以访问正在插值的属性,并使用它来移动点、更改线条粗细或 Core Graphics 让您控制的任何其他内容。看看 这个来自 Apple 的示例代码,看看它如何使用正在动画的 lineWidth 值。

约翰的这个小教程Blackburn 是我最喜欢的核心动画介绍之一。有一些不同的概念需要掌握,但总的来说,创建相当复杂的动画非常简单。

您的运行速度越来越慢的问题很可能与核心显卡无关。例如,您可能不小心创建了越来越多的计时器。

You should use the Core Animation framework to animate properties on the layer. You can either subclass CALayer or use a delegate to do the actual drawing. In the drawing code, you can access the property that is being interpolated and use it to move points, change the thickness of lines, or anything else that Core Graphics lets you control. Look at this example code from Apple and see how it uses the lineWidth value that is being animated.

This little tutorial by John Blackburn is one of my favorite introductions to Core Animation. There are a few different concepts to grasp, but in general it's pretty simple to create quite complex animations.

Your problem with things running slower and slower most probably has nothing to do with Core Graphics. It could be that you are accidentally creating more and more timers for example.

埋情葬爱 2024-09-18 03:57:32

如果您有一条需要从一个状态到另一个状态进行动画处理的路径,我强烈建议您查看 CAShapeLayer。此图层类型允许您指定两条路径(具有相同数量的控制点)并使用核心动画在它们之间平滑地制作动画。

关于这个主题的两篇好文章是 “Marching Ants With核心动画” 作者:Matt Long 和 “使用 CAShapeLayer 进行复杂插值(免费)”,作者:Joe Ricioppo。

如果您的图像部分保持不变,并且您只需要在其上合成新元素,我建议将图像的一部分绘制到静态 CALayers 或 UIViews 中,然后在其上添加新视图发生变化的部分。图层的合成比重绘视图或图层的内容要快得多。

If you have a path that needs to animate from state to state, I'd highly recommend looking at CAShapeLayer. This layer type lets you specify two paths (with the same number of control points) and animate smoothly between them using Core Animation.

Two good articles on the subject are "Marching Ants With Core Animation" by Matt Long and "Complex Interpolation with CAShapeLayer (Free)" by Joe Ricioppo.

If you instead have portions of the image that remain the same, and you just need to composite new elements on top of that, I'd recommend drawing parts of the image into static CALayers or UIViews and then adding new views on top of that for the parts that change. Compositing of layers is much, much faster than redrawing the content of a view or layer.

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