【问题标题】:NSView drawRect interfering with subviews?NSView drawRect 干扰子视图?
【发布时间】:2011-04-21 15:00:02
【问题描述】:

我有一个 nsview,我使用 draw rect 为背景绘制图像。它还有 3 个子视图 nsbuttons。问题是,只要鼠标按下按钮,其他按钮就会消失。但是当我删除 draw rect 方法时,这不会发生。所以我猜这与绘制图像的draw rect方法有关。

我怎样才能避免这种情况? 谢谢。

编辑: 好的,我知道问题出在哪里了。基本上,我有一个 NSMenuItem,我在里面放了一个带有 3 个按钮的视图。但是在 NSMenu 的顶部,有一个 4 像素的填充。因此,基本上,要删除该填充,我使用了此处提供的解决方案: Gap above NSMenuItem custom view

从解决方案中,drawRect方法中有一行:

[[NSBezierPath bezierPathWithRect:fullBounds] setClip];

那一刻,我删除了这条线,按钮正常运行。但是,顶部的填充并没有消失。

这是我的drawRect:

- (void) drawRect:(NSRect)dirtyRect {

    [[NSGraphicsContext currentContext] saveGraphicsState];

    NSRect fullBounds = [self bounds];
    fullBounds.size.height += 4;
    [[NSBezierPath bezierPathWithRect:fullBounds] setClip];

    NSImage *background = [NSImage imageNamed:@"bg.png"];
    [background drawInRect:fullBounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:100.0];

    [[NSGraphicsContext currentContext] restoreGraphicsState];
}

【问题讨论】:

  • 你能发布你的自定义drawRect:吗?
  • 发布了drawRect方法。谢谢。

标签: objective-c cocoa nsview drawrect nsbutton


【解决方案1】:

链接问题的解决方案不包括保存和恢复图形状态,当您修改不是您创建的图形时,这是一个好主意。试试这个:

- (void)drawRect:(NSRect)dirtyRect {
   // Save the current clip rect that has been set up for you
   [NSGraphicsContext saveGraphicsState];
   // Calculate your fullBounds rect
   // ...
   // Set the clip rect
   // ...
   // Do your drawing
   // ...
   // Restore the correct clip rect
   [NSGraphicsContext restoreGraphicsState]

【讨论】:

  • 感谢您的回复,但没有奏效。另外,我认为您的意思是 saveGraphicsState 和 restoreGraphicsState?再次感谢。
  • @user635064:我确实是这个意思,是的,谢谢;我整天都在打错字。即使它不能直接解决您的问题,您仍然应该使用它;它会阻止其他人。
【解决方案2】:

您确定这些按钮实际上是子视图,而不仅仅是放置在您正在绘制的视图上吗?

【讨论】:

  • 感谢您的回复。是的,我确定,但是,我知道是什么导致了问题,但我不知道如何解决它。请再次阅读我的原始问题,因为我对其进行了编辑并详细解释了问题。再次感谢。
猜你喜欢
  • 2013-02-16
  • 1970-01-01
  • 2013-07-23
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多