To clarify the purpose of this question: I know HOW to create complicated views with both subviews and using drawRect. I'm trying to fully understand the when's and why's to use one over the other.个
我也明白,提前优化那么多,并在进行任何分析之前以更困难的方式做一些事情是没有意义的.考虑到我对这两种方法都很满意,现在我真的很想更深入地了解这两种方法.
我在念力上学到的很多东西都来自于学习如何让表格视图滚动性能变得真正流畅和快速.当然,这个方法的最初来源是iPhone的author behind twitter(以前是twetie).Basically it says that to make table scrolling buttery smooth, the secret is to NOT use subviews, but instead do all the drawing in one custom uiview.从本质上讲,使用大量的子视图似乎会降低渲染速度,因为它们的开销很大,并且会不断地在父视图上重新组合.
To be fair, this was written when the 3GS was pretty brand spankin new, and iDevices have gotten much faster since then. Still this method is regularly suggested on the interwebs and elsewhere for high performance tables. In fact it's a suggested method in Apple's Table Sample Code, has been suggested in several WWDC videos (Practical Drawing for iOS Developers), and many iOS programming books.
甚至还有awesome looking tools个来设计图形并为它们生成核心图形代码.
So at first I'm lead to believe "there’s a reason why Core Graphics exists. It’s FAST!"
但是,一旦我认为我得到了"可能的时候支持核心图形"的 idea ,我就开始意识到,draRect往往是应用程序响应能力差的罪魁祸首,内存非常昂贵,而且真的会给CPU带来负担.基本上,我应该"Avoid overriding drawRect"(WWDC 2012 iOS App Performance: Graphics and Animations)
所以我想,就像所有事情一样,这很复杂.也许你可以帮助我自己和其他人理解使用drawRect的时间和原因?
我发现使用核心图形有两种明显的情况:
- 您有动态数据(苹果的股票图表示例)
- 你有一个灵活的UI元素,不能用一个简单的可调整大小的图像来执行
- 您正在创建一个动态图形,渲染后可在多个位置使用该图形
我看到了避免核心显卡的情况:
- 您的视图属性需要单独设置动画
- 您的视图层次 struct 相对较小,因此任何使用CG的额外工作都是不值得的
- 您希望更新视图的各个部分,而不重新绘制整个视图
- 当父视图大小更改时,需要更新子视图的布局
So bestow your knowledge. In what situations do you reach for drawRect/Core Graphics (that could also be accomplished with subviews)? What factors lead you to that decision? How/Why is drawing in one custom view recommended for buttery smooth table cell scrolling, yet Apple advises drawRect against for performance reasons in general? What about simple background images (when do you create them with CG vs using a resizable png image)?
制作有价值的应用程序可能不需要对这个主题有深刻的理解,但我不喜欢在不能解释原因的情况下在两种技术之间进行 Select .我的大脑开始生我的气.
问题更新
谢谢大家提供的信息.这里有一些澄清的问题:
- 如果您正在使用核心图形绘制一些东西,但是可以使用UIImageViews和预渲染的PNG来完成相同的事情,那么您是否应该总是这样做呢?
- 类似的问题:尤其是在badass tools like this的时候,你应该考虑在核心图形中绘制界面元素吗?(可能是当您的元素显示可变时.例如,一个按钮有20种不同的 colored颜色 变化.还有其他情况吗?)
- 根据我在下面回答中的理解,通过在复杂的UIView渲染本身之后有效地捕获单元格的快照位图,并在滚动和隐藏复杂视图的同时显示该快照位图,是否有可能获得表格单元格的相同性能提升?显然,有些部分需要解决.我有一个有趣的 idea .