Optimize Core Graphics animated drawing (iPhone)

落爺英雄遲暮 提交于 2019-12-03 20:17:10

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!