绘制矩形与布局子视图 - NSView - Cocoa touch
我找不到一个合适的答案来解释这两个函数之间的区别。 每个人什么时候接到电话,其中一个与另一个有何不同?
例如,我不能只在drawrect 中布局我的视图吗?
谢谢
I can't find a decent answer to explain me the difference between these 2 functions.
when does every one gets called, and how does one different then the other ?
for example , can't I just layout my views inside drawrect ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果设置了“需要布局”标志,则从
-layoutIfNeeded
调用-layoutSubviews
(使用-setNeedsLayout
,或在视图边界更改时自动) 。使用它来定位您的视图[编辑:使用它来定位子视图]。如果设置了“需要显示”标志(使用
-setNeedsDisplay
),则从-displayIfNeeded
调用-drawRect:
,或者如果设置则自动调用>view.contentMode = UIViewContentModeRedraw
)。-layoutIfNeeded
和-displayIfNeeded
都会在绘制到屏幕之前由 UIKit/CoreAnimation 自动调用;你很少需要直接打电话给他们。您可以将子视图放置在
-drawRect:
中(您甚至可以添加子视图!),但这是不明智的:-setNeedsDisplay
。-drawRect:
会降低性能(UIKit/CoreAnimation 必须为您创建位图支持的图形上下文);仅当您需要执行自定义绘图时才这样做。-drawRect:
中重绘视图。画画很贵。移动视图很便宜。编辑:当我醒来时,还有一些更多细节:
“显示”和“绘制”之间有什么区别?显示是通过
-[CALayer显示]
来完成的;默认实现是(大约)-displayLayer:
,则调用[self.delegate displayLayer:self]
。-displayLayer:
应该将layer.content
设置为(例如)CGImage,-drawLayer:inContext:
,设置一个位图支持的上下文,调用[self.delegate drawLayer:self inContext:context]
,并将输出保存到layer.content
(输出实际上是CABackingStore,可能是私有 API)layer.content
。视图是图层的委托,因此您可以改为实现
-[MyView displayLayer:]
,并执行一些有趣的操作,例如self.layer.contents = (id)([UIImage imageNamed:@" foo"].CGImage)
(这大致就是 UIImageView 所做的)-layoutSubviews
is called from-layoutIfNeeded
if the "layout needed" flag was set (using-setNeedsLayout
, or automatically when the view bounds changes). Use it for positioningyour view[EDIT: Use it for positioning subviews].-drawRect:
is called from-displayIfNeeded
if the "display needed" flag was set (using-setNeedsDisplay
, or automatically if you setview.contentMode = UIViewContentModeRedraw
).Both
-layoutIfNeeded
and-displayIfNeeded
are called automatically by UIKit/CoreAnimation before things are drawn to screen; you rarely need to call them directly.You can position your subviews in
-drawRect:
(you can even add subviews!), but this is unwise:-setNeedsDisplay
is not called automatically on a bounds change.-drawRect:
reduces performance (UIKit/CoreAnimation has to create a bitmap-backed graphics context for you); only do so if you need to perform custom drawing.-drawRect:
. Drawing is expensive. Moving views around is cheap.EDIT: And some more detail when I'm awake:
What's the difference between "display" and "draw"? Displaying is done by
-[CALayer display]
; the default implementation is (approximately)-displayLayer:
, call[self.delegate displayLayer:self]
.-displayLayer:
is supposed to setlayer.content
to (e.g.) a CGImage,-drawLayer:inContext:
, set up a bitmap-backed context, call[self.delegate drawLayer:self inContext:context]
, and save the output tolayer.content
(the output is actually a CABackingStore, which is presumably a private API)layer.content
.The view is the layer's delegate, so you can implement
-[MyView displayLayer:]
instead, and do interesting stuff likeself.layer.contents = (id)([UIImage imageNamed:@"foo"].CGImage)
(which is roughly what UIImageView does)也许我没有所有的答案,但我可以告诉你:
- 当视图框架改变时,layoutSubviews被调用
- 当 setNeedDisplay 被调用时,drawRect 被调用
大多数时候你不会使用layoutSubviews。
可能还有其他情况,但它可以给你第一个答案^^
祝你好运
Maybe I don't have all the answers, but I can say you that :
- layoutSubviews is called when the view frame is changed
- drawRect is called when setNeedDisplay is called
Most of the time you won't use layoutSubviews.
There may be others cases, but it could give you a first piece of answer ^^
Good luck