透明窗口中的圆角NSView [英] Rounded NSView in a Transparent Window

查看:1030
本文介绍了透明窗口中的圆角NSView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个透明的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 {
//使用NSBorderlessWindowMask结果在没有标题栏的窗口中。
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if(self!= nil){
//开始没有透明度的所有绘制到窗口
[self setAlphaValue:1.0];
//关闭不透明度,使窗口中未绘制的部分是透明的。
[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];
}

任何人都可以告诉我我在这里缺少什么?



感谢。

解决方案

,其中有一个红色轮廓(笔触),但中心区域是透明的?





如果是这样,我使用了以下代码:

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

如果这是你要找的,你可以使用初始点。你会想要确保你插入框架 rect的一半行程宽度,以避免剪裁的角落,如你所看到的问题。 / p>

I'm trying to make a transparent NSWindow with a rounded view in there.

I'm trying to have a rounded view with a transparent window.

This is what it looks like now: (see the little dots in the corners)

Here's another example with the border radius set to 10px (set in NSView drawRect):

I am using code from this Apple sample: https://developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html

Specifically this method in my NSWindow subclass:

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

And this in my NSView subclass:

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

Can anyone tell me what I'm missing here?

Thanks.

解决方案

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

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.

这篇关于透明窗口中的圆角NSView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆