我在 drawRect
中使用 UIGraphicsGetImageFromCurrentContext
并将 subview 的属性设置为返回的 UIImage
。这最终会触发 subview 中的 setNeedsDisplay
。
这有什么副作用吗?
基本上,如果在父 View 的drawRect
方法中调用了setNeedsDisplay
,那么 subview 是否总是会调用其drawRect
方法?或者 subview 是否会被视为“已显示”,这意味着不会发生更新?
最佳答案
来自邮件列表:http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01697.html
If you call setNeedsDisplay: from within drawRect:, the flag will be set, but as soon as drawRect: returns, it will be cleared again. Therefore, when the time arrives, the flag is clear and drawRect: is not called again.
-drawRect should only be used for drawing. Resizing, repositioning, and adding or removing subviews absolutely should not be done in drawRect:. Incorrect behavior will likely result. The AppKit reasonably relies on the view hierarchy remaining constant while it is in the middle of drawing the view hierarchy.
Furthermore, do not call -display or variants or -drawRect from within drawRect: or you invite an infinite recursion and crash.
上面的代码片段回答了这个问题:http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01693.html
Overview
I have an NSView (viewA) that manages multiple subviews. This view's drawRect contains code that positions and sizes all of its subviews accordingly and calls each of the subview's setNeedsDisplay:YES method. One of the subviews is also an NSView (viewB), and it also has multiple subviews.
Problem
If the window that contains viewA is resized, viewA's drawRect method is invoked, but for some unknown reason, viewB's drawRect method is not always invoked, despite the fact that viewA's drawRect method invokes viewB's setNeedsDisplay:YES method. The drawRect method associated with all of viewA's other subviews are invoked though.
Question
Can anyone suggest any reasons as to why viewB's drawRect method is not always called after its superview sends it a setNeedsDisplay message? Any ideas on what I'm doing wrong would be appreciated.
关于ios - 在 [superview drawRect :]? 内调用 [subview setNeedsDisplay] 是否安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41292432/