2013-02-21 78 views
4

我试图在圆角视图中制作一个透明的NSWindow。透明窗口中的圆角NSView

我想要一个透明窗口的圆角视图。

这是它看起来像现在:(见角的小圆点)

enter image description here

下面是与(在一个NSView的drawRect集)设置为10px圆角半径另一个例子:

enter image description here

我使用代码从这款苹果样本: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]; 
} 

谁能告诉我什么,我在这里失踪?

谢谢。

+1

没有尝试,但使用QuartzCore,并设置cornerRadius(在CALayer)? – Larme 2013-02-21 17:08:08

回答

3

您是否在寻找类似以下内容的地方,哪里有红色轮廓(笔划),但中心区域是透明的?

enter image description here

如果是这样,要实现这一点,我用下面的代码:

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

如果这是你在找什么,你也许可以把它作为一个起点。您需要确保您将冲程线宽度的一半插入,以避免像看到的那样夹角的问题。

+0

谢谢!我不需要中心区域是透明的,但它只是一个文本字段。无论如何,这段代码工作正常! – Wesley 2013-02-21 18:10:20

3

不知道这是否就是你要找的,但有一个伟大的阶级由Matt Gemmell称为MAAttachedWindow并且可以在这里找到:http://mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass/

这是年纪大一点的,但对我来说仍然伟大工程,当我需要做一个“浮动”弹出窗口并配置透明度,边框半径,如果需要,甚至可以为上下文添加一个小箭头。我用它所有的时间。

+0

谢谢,我会看看! – Wesley 2013-02-21 18:11:35