2011-04-21 62 views
5

我有一个nsview,我使用绘制矩形为背景绘制图像。它也有3个子视图nsbuttons。问题是,只要鼠标停在按钮上,其他按钮就会消失。但是,当我删除绘制矩形方法时,这不会发生。所以我猜测这与绘制图像的绘制矩形方法有关。NSView drawRect干扰子视图?

我该如何避免这种情况? 谢谢。编号: 好吧,我想出了问题出在哪里。基本上,我有一个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]; 
} 
+0

你可以发布你的自定义'drawRect:'吗? – 2011-04-21 20:23:07

+0

发布drawRect方法。谢谢。 – user635064 2011-04-21 21:02:19

回答

0

你确定这些按钮实际上是子视图,而不是仅仅放在你绘制的看法?

+0

感谢您的回复。是的,我确定,但是,我想到了什么是导致问题的原因,我不知道如何解决这个问题。请再次阅读我原来的问题,因为我编辑了它并详细解释了问题。再次感谢。 – user635064 2011-04-21 16:14:59

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] 
+0

感谢您的回复,但没有奏效。另外,我认为你的意思是saveGraphicsState和restoreGraphicsState?再次感谢。 – user635064 2011-04-21 19:54:59

+0

@ user635064:我的意思是,是的,谢谢;我整天都在拼错。即使它不直接解决您的问题,您仍应该使用它;它会阻止其他人。 – 2011-04-21 20:22:48

+0

谢谢,会做。 – user635064 2011-04-21 21:03:13