繁体   English   中英

在透明窗口中舍入NSView

[英]Rounded NSView in a Transparent Window

我正在尝试制作一个带有圆形视图的透明NSWindow。

我正在尝试使用透明窗口进行圆角处理。

这就是现在的样子:(请看角落中的小点)

在此处输入图片说明

这是边界半径设置为10px(在NSView drawRect中设置)的另一个示例:

在此处输入图片说明

我正在使用此Apple示例中的代码: https : //developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html

具体来说,此方法在我的NSWindow子类中:

- (id)initWithContentRect:(NSRect)contentRect
                styleMask:(NSUInteger)aStyle
                  backing:(NSBackingStoreType)bufferingType
                    defer:(BOOL)flag {
    // Using NSBorderlessWindowMask results in a window without a title bar.
    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
    if (self != nil) {
        // Start with no transparency for all drawing into the window
        [self setAlphaValue:1.0];
        // Turn off opacity so that the parts of the window that are not drawn into are transparent.
        [self setOpaque:NO];
    [self setBackgroundColor:[NSColor clearColor]];

    }
    return self;
}

这在我的NSView子类中:

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor redColor] set];
    NSBezierPath* thePath = [NSBezierPath bezierPath];
    [thePath appendBezierPathWithRoundedRect:dirtyRect xRadius:3 yRadius:3];
    [thePath fill];
}

谁能告诉我我在这里想念的东西吗?

谢谢。

不确定这是否是您要查找的内容,但是Matt Gemmell有一个很棒的类叫做MAAttachedWindow,可以在这里找到: http ://mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass/

它有些旧,但是当我需要执行“浮动”弹出窗口并配置透明度,边框半径,甚至需要时为上下文添加小箭头时,对我来说仍然非常有用。 我用它所有的时间。

您是否正在寻找类似以下的东西,那里有一个红色的轮廓(描边),但中心区域是透明的?

在此处输入图片说明

如果是这样,为了实现这一目标,我使用了以下代码:

- (void)drawRect:(NSRect)frame {
    frame = NSInsetRect(self.frame, 3.0, 3.0);

    [NSBezierPath setDefaultLineWidth:6.0];

    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame
                                             xRadius:6.0 yRadius:6.0];
    [[NSColor redColor] set];
    [path stroke];
}

如果这是您要寻找的内容,则可以以此为起点。 您需要确保将frame插入笔触线宽度的一半,以免像您看到的那样修剪角。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM