使用NSBezierPath在NSView上绘制圆角很差 [英] rounded corners on NSView using NSBezierPath are drawn badly

查看:303
本文介绍了使用NSBezierPath在NSView上绘制圆角很差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ViewController的主NSView中,我重写func drawRect(dirtyRect: NSRect)方法以使用NSBezierPath在主视图上实现圆角. 用同样的方法,我还指定了主视图的渐变背景.

In my ViewController's main NSView, I override the func drawRect(dirtyRect: NSRect) method to implement rounded corners on my main view using NSBezierPath. In that same method I also designate the gradient background of my main view.

override func drawRect(dirtyRect: NSRect) {

    let path: NSBezierPath = NSBezierPath(roundedRect: self.bounds, xRadius: 18.0, yRadius: 18.0)
    path.addClip()

   let gradient = NSGradient(startingColor: NSColor(hexColorCode: "#383838"), endingColor: NSColor(hexColorCode: "#222222"))
   gradient.drawInRect(self.frame, angle: 90)
}

出现的问题如下图所示:

The problem that arises is illustrated in the following image:

图像显示了视图角之一.圆角的倒圆只是部分成功,因为仍然有一个白色的圆角伸出窗口的圆角之外.

The image shows one of the views corners. The rounding of corners is only partially successful, as there still remains a white corner sticking out beyond the window's rounded corners.

如果有人有更好的方法来设置窗口的拐角半径,我会接受这样的建议.我对此事进行了大量研究,但是这种解决方案似乎是最简单的.

If anyone has a better method of setting a window's corner radius I would be open to such suggestions. I have done a lot of research on the matter, however this solution appears to be the simplest.

非常感谢您提供有关解决此问题的建议.

Any advice on how to fix this issue is greatly appreciated.

推荐答案

您应在NSWindow实例上执行以下操作:

You should do the following on your NSWindow instance:

[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];

并绘制所需的形状.

并查看这篇文章.

这篇关于使用NSBezierPath在NSView上绘制圆角很差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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