Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 防止NSButtonCell图像在其包含的NSScrollView之外绘制_Cocoa_Core Graphics_Nsimage_Nsscrollview_Nsbuttoncell - Fatal编程技术网

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

Cocoa 防止NSButtonCell图像在其包含的NSScrollView之外绘制,cocoa,core-graphics,nsimage,nsscrollview,nsbuttoncell,Cocoa,Core Graphics,Nsimage,Nsscrollview,Nsbuttoncell,我曾经(并回答)过一个非常类似的问题。这个问题之所以能够解决,是因为我知道dirtyRect,因此知道应该在哪里绘制图像。现在,我在一个子类NSButtonCell中看到了相同的行为: 在我的子类中,我只是覆盖drawImage:withFrame:inView方法来添加阴影 - (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView { NSGraphicsCo

我曾经(并回答)过一个非常类似的问题。这个问题之所以能够解决,是因为我知道
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
通过使用自己的剪辑路径工作,在绘制图像之前,您将该路径吹走


这也让我绊倒了很长一段时间。

你不应该在
NSBezierPath上调用
-setClip
,除非你是认真的。通常,您需要
-addClip
,它将剪切路径添加到当前剪切区域集

NSScrollView
通过使用自己的剪辑路径工作,在绘制图像之前,您将该路径吹走

这也让我绊倒了很长一段时间