防止NSButtonCell图像在其包含的NSScrollView之外绘制

我之前曾问过(并回答过)一个非常类似的问题。这个问题能够解决,因为我知道
dirtyRect
,因此知道我应该在哪里画图像。现在,我看到了与子类
NSButtonCell
相同的行为: 在我的子类中,我简单地重写了
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];
} 它似乎都非常标准,但图像仍然在包含scrollview边界之外绘制。我怎么能避免这个?     
已邀请:
你不应该在
NSBezierPath
上叫
-setClip
,除非你真的是这个意思。通常你想要
‑addClip
,它将你的剪切路径添加到当前剪辑区域的集合。
NSScrollView
使用自己的剪切路径工作,然后在绘制图像之前将其吹走。 这让我绊倒了很长时间。     

要回复问题请先登录注册