2011-04-21 115 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