gpt4 book ai didi

cocoa - 带有圆角的自定义 NSWindow 可以剪辑 subview

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

我正在尝试创建一个自定义 NSWindow,因此我创建了一个具有适当的无边框窗口蒙版并且可以正常工作的窗口。我提供我自己的内容 View ,这很好。但我想做的是用圆角绘制,将 subview 剪辑到这些角上。这可能吗?

在我的内容 View 中,我可以覆盖 drawRect: 并绘制带圆角的路径,但是当我向其中添加 subview 时,它们不会被剪切。

我可以改为让我的内容 View 受到图层支持并给它一个角半径(将masksToBounds设置为YES),但是当我添加 subview 时,它们仍然是没有被我的圆角剪掉。

有办法做到这一点吗?或者有什么方法可以绘制一个没有标题栏的 NSWindow,并且我可以完全控制绘图,同时保持圆角、剪切角?

最佳答案

我能够做的是提供我的 NSWindow 的自定义子类:

@implementation ELGRoundWindow

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:flag];

if ( self )
{
[self setStyleMask:NSBorderlessWindowMask];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
}

return self;
}


- (void) setContentView:(NSView *)aView
{
aView.wantsLayer = YES;
aView.layer.frame = aView.frame;
aView.layer.cornerRadius = 20.0;
aView.layer.masksToBounds = YES;


[super setContentView:aView];

}

@end

然后在 IB 中,我将内容 View 的类更改为 ELGoundView:

@implementation ELGRoundView

- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor colorWithCalibratedRed:0.0 green:0.5 blue:1 alpha:1] set];
NSRectFill(dirtyRect);
}

@end

我在内容 View 中放置了另一个方形 subview ,内容如下:

@implementation ELGSquareView

- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor colorWithCalibratedRed:0.0 green:0 blue:1 alpha:1] set];
NSRectFill(dirtyRect);
}

@end

我最终得到:

Rounded window with clipped subview

关于cocoa - 带有圆角的自定义 NSWindow 可以剪辑 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15179288/

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