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
通过使用自己的剪辑路径工作,在绘制图像之前,您将该路径吹走
这也让我绊倒了很长一段时间