优化核心图形动画(iPhone) [英] Optimize Core Graphics animated drawing (iPhone)

查看:140
本文介绍了优化核心图形动画(iPhone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



<我要求其他方法来制作动画。什么是最有效的方式和原因?

解决方案

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



这约翰布莱克本的小教程是我最喜欢的核心动画介绍之一。有一些不同的概念需要掌握,但一般情况下创建相当复杂的动画非常简单。



你运行速度越来越慢的问题很可能无关紧要与核心图形。可能是你不小心创造了越来越多的计时器。


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?

解决方案

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.

这篇关于优化核心图形动画(iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆