gpt4 book ai didi

cocoa - 使用 -setClip 方法在自定义 View 上重绘插图 NSShadow

转载 作者:行者123 更新时间:2023-12-03 16:26:07 25 4
gpt4 key购买 nike

我有一个奇怪的问题,与这个问题的答案相关:

Draw an Inset NSShadow and Inset Stroke

我使用此代码到自定义 View 的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];
}
}

当我添加多行标签(自定义 View 的同级或子级)时,问题就出现了。

当我的程序窗口失去焦点并且我回到它时,我的内部阴影/描边会变暗。看起来阴影重叠了。这很奇怪,因为如上所述,如果我的窗口只有这个自定义 View ,那么一切都会顺利。

如果我评论该行

[path setClip]; 

阴影不再叠加,但我没有得到所需的圆角效果(类似于 NSBox)。

我尝试了使用按钮而不是多行标签会发生什么,并且通过丢失/获取窗口焦点,它没有问题,但是当我单击按钮时,阴影会叠加。

我发现问题与这里类似,但是是在 Cocoa 而不是 Java 中:

Java setClip seems to redraw

感谢您的帮助!

最佳答案

除非您知道自己在做什么,否则永远不要使用-setClip。您应该使用 -addClip 来代替,它会尊重现有的剪切路径。

关于cocoa - 使用 -setClip 方法在自定义 View 上重绘插图 NSShadow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8535613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com