ios - 在 [superview drawRect :]? 内调用 [subview setNeedsDisplay] 是否安全

标签 ios uiview uikit drawrect

我在 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/

相关文章:

ios - Swift 3 - 如何让计时器在后台工作

ios - 在 Swift 中使用 NSLayoutConstraint 将多个 UIView 放置在视觉时间轴上

cocoa-touch - iOS导航栏更喜欢大标题滚动行为

iphone 在圆圈上绘制阴影

ios - Table tap 和 didselect 方法冲突

ios - 自定义 Controller 包含和导航栏高度

ios - 如何禁用 UITextField 的编辑属性?

ios - 使用 iOS SDK 将多张照片作为单个帖子上传,例如时间轴照片,而不是 Facebook 中的相册

ios - 如何创建免费的 iOS 开发配置文件

Kurento 教程的 iOS 客户端