iPhone - UIView 添加子视图与drawRect?
将视图添加为子视图与在该视图的 drawRect 方法中绘制视图之间有什么区别?
我使用这两种方法,但我想知道除了明显的方法之外,例如,如果您在drawRect中这样做,它将是单个视图,而不是两个视图,或者添加为子视图更容易。是否有任何特定情况下您绝对应该使用其中一种而不是另一种?
What are the differences between adding a view as a subView VS drawing the view in that view's drawRect method?.
I use both approaches, but I was wondering apart from the obvious such as, if you do in drawRect it will be a single View, as opposed as two views, or adding as a subview is just easier. Are there any specific situations where you should definitely use one over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重写
-drawRect:
可能是绘制复杂UITableViewCell
的好方法。将所有子视图合成并绘制为一个视图有助于极大地提高表视图滚动性能。话虽如此,我通常会坚持使用可用的最高级别 API,只有在性能受到影响时才会降低到较低级别。
Overriding
-drawRect:
can be a good way to draw complexUITableViewCell
s. Having all of the subviews composited and drawn as one view helps table view scrolling performance immeasurably.Having said that, I typically stick with the highest level API available and drop down to a lower level only if performance suffers.
正如您所指出的,添加子视图更容易,而且我确实认为在 90% 的情况下这是没有争议的。您通常应该添加一个子视图,并让库在正确的位置处理子视图的绘制。
我只使用
-drawRect:
在视图中完成自定义绘制,而不是绘制子视图,它会产生不必要的复杂性。如果您需要大量性能,-drawRect:
可以帮助您。另外,在简单绘图的情况下,-drawRect:
非常好,而不是制作多个子视图。但一般来说,只添加一个子视图是值得的。如果将来您决定希望这些子视图接收触摸事件或交互处理事物,则重构您的
-drawRect:
代码会更加困难。Adding a subview is easier as you point out, and I really see this as no contest in 90% of cases. You should generally add a subview and let the libraries handle drawing of subviews in their correct position.
I only use
-drawRect:
to accomplish custom drawing within my view, but not to draw subviews, it creates unnecessary complexity. If you need a lot of performance,-drawRect:
can help you there. Also, in the case of simple drawing,-drawRect:
is very nice as opposed to making several subviews. But in general, it pays to just add a subview.If in the future you decide you want these subviews to receive touch events or handle things interactively, it is more difficult to refactor your
-drawRect:
code.