2011-03-08 25 views
0

我有asked(並回答)一個非常類似的問題。這個問題能夠解決,因爲我知道dirtyRect,因此,知道我應該在哪裏畫圖像。現在,我看到相同的行爲與子類NSButtonCell防止NSButtonCell圖像在其包含NSScrollView以外的繪圖

enter image description here

在我的子類,我只是重寫drawImage:withFrame:inView方法添加了一層陰影。

- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView { 

    NSGraphicsContext *ctx = [NSGraphicsContext currentContext]; 
    [ctx saveGraphicsState]; 

    // Rounded Rect 
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame cornerRadius:kDefaultCornerRadius]; 
    NSBezierPath *shadowPath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 0.5, 0.5) cornerRadius:kDefaultCornerRadius]; // prevents the baground showing through the image corner 

    // Shadow 
    NSColor *shadowColor = [UAColor colorWithCalibratedWhite:0 alpha:0.8]; 
    [NSShadow setShadowWithColor:shadowColor 
        blurRadius:3.0 
         offset:NSMakeSize(0, -1)]; 
    [[NSColor colorWithCalibratedWhite:0.635 alpha:1.0] set]; 
    [shadowPath fill]; 
    [NSShadow clearShadow]; 

    // Background Gradient in case image is nil 
    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[UAColor grayColor] endingColor:[UAColor lightGrayColor]]; 
    [gradient drawInBezierPath:shadowPath angle:90.0]; 
    [gradient release]; 

    // Image 
    if (image) { 
     [path setClip]; 
     [image drawInRect:frame fromRect:CGRectZero operation:NSCompositeSourceAtop fraction:1.0]; 
    } 

    [ctx restoreGraphicsState]; 

}

似乎都相當標準,但圖像依然繪製包含滾動視圖範圍之外。我怎樣才能避免這種情況?

回答

4

您不應該撥打電話-setClipNSBezierPath,除非您真的真的是的意思。一般而言,您需要‑addClip,它將您的剪切路徑添加到當前剪切區域集合中。

NSScrollView通過使用其自己的剪切路徑工作,並在繪製圖像之前將該路徑吹走。

這也讓我絆了很久。