drawRect 的奇怪行为:覆盖 NSView?
大家好,
首先感谢大家帮助我解决了我之前的问题。
当覆盖 NSView 的 drawRect: 时,我看到奇怪的行为。我有一个非常简单的 NSView 子类(RoundedView),它只覆盖了drawRect。我已在界面生成器中将自定义视图的类设置为 RoudedView。在该视图中我有一个标签。 drawRect: 按预期正确调用,但令我惊讶的是,该视图内的 Label 也调用了 RoundedView drawRect: 。这导致标签的矩形由仅用于自定义视图而不是用于该视图中的任何其他元素的drawRect绘制。因此,标签的矩形不正确。自定义视图位于 styleMask 设置为 NSBorderlessWindowMask 的窗口内。
这是正确的行为吗?如何防止为属于视图一部分的元素调用drawRect?
我已经浏览了堆栈上的所有相关帖子,但不幸的是,结果是空的。
谢谢,
罗伯特
Greetings all,
First of all thank you all for helping me out with my previous questions.
I see strange behavior when overriding drawRect: for NSView. I have a very simple NSView subclass (RoundedView) which only overrides drawRect. I have set the class of a custom view to RoudedView in interface builder. Inside that view I have a label. drawRect: is called correctly as expected but to my surprise, the RoundedView drawRect: is also called for the Label inside that view. This causes the rect for the label to be drawn by the drawRect that was only intended for the custom view and not for any other elements within that view. As a result the rect for the label is not correct. The custom view is inside a window that has the styleMask set to NSBorderlessWindowMask.
Is this correct behavior? How do I prevent drawRect being called for elements that are part of the view?
I have gone through all the related post here on the stack but unfortunately came up empty.
Thanks,
Robert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要期望传递给
drawRect:
的矩形始终是视图的边界。NSView
能够仅重绘自身需要重绘的部分(因此,该参数称为dirtyRect
)。在这种情况下,标签与视图的内容重叠,因此每当重绘时,视图系统也需要重绘其后面的视图(您的)。如果您的视图的绘制不是很复杂,您可以忽略传递给您的矩形并使用视图的边界,否则,您将需要弄清楚如何仅绘制视图中“脏”的部分。
Don't expect the rectangle passed to
drawRect:
to always be your view's bounds.NSView
is capable of redrawing only the portions of itself that need redrawing (therefore, the parameter is calleddirtyRect
). In this case, the label overlaps the content of your view, so whenever it is redrawn, the view system also needs to redraw the view behind it (yours).If your view's drawing isn't very complex, you can just ignore the rectangle that is passed to you and use the view's bounds instead, otherwise, you will need to figure out how to draw only the part of your view that is 'dirty'.