2011-12-16 33 views
2

我和奇怪的問題,這個問題的答案相關:Redrawed插圖NSShadow上自定義視圖使用-setClip方法

Draw an Inset NSShadow and Inset Stroke

我使用此代碼爲自定義視圖的drawRect方法。我正是這樣的:

- (void)drawRect:(NSRect)rect 
{ 
    // Create and fill the shown path 
    NSBezierPath *path = [NSBezierPath 
          bezierPathWithRoundedRect:[self bounds] 
          xRadius:4.0f 
          yRadius:4.0f]; 

    [[NSColor colorWithCalibratedWhite:0.8f alpha:0.2f] set]; 

    [path fill]; 

    // Save the graphics state for shadow 
    [NSGraphicsContext saveGraphicsState]; 

    // Set the shown path as the clip 
    [path setClip]; 

    // Create and stroke the shadow 
    NSShadow * shadow = [[[NSShadow alloc] init] autorelease]; 
    [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0f alpha:0.8f]]; 
    [shadow setShadowBlurRadius:2.0]; 
    [shadow set]; 
    [path stroke]; 

    // Restore the graphics state 
    [NSGraphicsContext restoreGraphicsState]; 

    if (highlight && [[self window] firstResponder] == self) { 
     NSSetFocusRingStyle(NSFocusRingOnly); 
     [[NSBezierPath bezierPathWithRect:[self bounds]] fill]; 
    } 
} 

問題出現在我添加多行標籤(兄弟或我的自定義視圖的子項)。

當我的程序窗口失去焦點,我回到它的時候,我的陰影/中風變得更暗。看起來陰影疊加。這很奇怪,因爲如前所述,如果我的窗戶只有這種自定義的視圖,那就很好。

如果我評論線

[path setClip]; 

的陰影不再疊加,但我沒有得到圓角的預期效果(比NSBox類似)。

我嘗試了使用按鈕而不是多行標籤發生的情況,並通過丟失/獲取窗口焦點它沒有問題,但是當我單擊按鈕時陰影被疊加。

我覺得這個問題是不是在這裏相似,但在可可而不是Java:

Java setClip seems to redraw

感謝您的幫助!

回答

5

除非您知道自己在做什麼,否則絕不能使用-setClip。您應該使用-addClip,因爲它尊重現有的剪切路徑。

+0

與setClip相同的效果(或相似)並且不重繪。我會考慮到它。謝謝! – 2011-12-19 08:53:51