4

我正在尝试制作一个带有圆形视图的透明 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];
}

谁能告诉我我在这里缺少什么?

谢谢。

4

2 回答 2

2

Are you looking for something like the following, where there's a red outline (stroke), but the center area is transparent?

enter image description here

If so, to achieve that, I used the following code:

- (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];
}

If that's what you're looking for, you can probably use that as a starting point. You'll want to make sure that you inset the frame rect one half of the stroke line width, so as to avoid the problem with clipping the corners like you were seeing.

于 2013-02-21T18:02:20.113 回答
2

不确定这是否是您正在寻找的,但 Matt Gemmell 有一个很棒的课程,名为 MAAttachedWindow,可以在这里找到:http: //mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass/

它有点旧,但当我需要做一个“浮动”弹出窗口并配置透明度、边框半径,甚至在需要时为上下文添加一个小箭头时,它仍然对我很有用。我用它所有的时间。

于 2013-02-21T17:52:32.927 回答