5

我有一个 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];
}
4

2 回答 2

3

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

- (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]
于 2011-04-21T17:43:25.853 回答
0

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

于 2011-04-21T15:18:12.660 回答