NSBezierPath圆角矩形有坏角

发布于 2024-11-02 08:27:43 字数 915 浏览 1 评论 0原文

我有一个 NSBezierPath ,它制作了一个圆角矩形,但它的角看起来不连贯,并且在全尺寸查看时比笔画的其余部分显得更亮。我的代码是:

 NSBezierPath *path = [NSBezierPath bezierPath];
    [path appendBezierPathWithRoundedRect:NSMakeRect(0, 0, [self bounds].size.width, [self bounds].size.height) xRadius:5 yRadius:5];

    NSGradient *fill = [[NSGradient alloc] initWithColorsAndLocations:[NSColor colorWithCalibratedRed:0.247 green:0.251 blue:0.267 alpha:0.6],0.0,[NSColor colorWithCalibratedRed:0.227 green:0.227 blue:0.239 alpha:0.6],0.5,[NSColor colorWithCalibratedRed:0.180 green:0.188 blue:0.196 alpha:0.6],0.5,[NSColor colorWithCalibratedRed:0.137 green:0.137 blue:0.157 alpha:0.6],1.0, nil];
    [fill drawInBezierPath:path angle:-90.0];

    [[NSColor lightGrayColor] set];
    [path stroke];

这是两个角的图片(在小图片中不那么明显):

corners

任何人都知道是什么原因造成的?我只是错过了什么吗?

感谢您的帮助

I have an NSBezierPath that makes a rounded rectangle but the corners of it look choppy and appear brighter that the rest of the stroke when viewed at full scale. My code is:

 NSBezierPath *path = [NSBezierPath bezierPath];
    [path appendBezierPathWithRoundedRect:NSMakeRect(0, 0, [self bounds].size.width, [self bounds].size.height) xRadius:5 yRadius:5];

    NSGradient *fill = [[NSGradient alloc] initWithColorsAndLocations:[NSColor colorWithCalibratedRed:0.247 green:0.251 blue:0.267 alpha:0.6],0.0,[NSColor colorWithCalibratedRed:0.227 green:0.227 blue:0.239 alpha:0.6],0.5,[NSColor colorWithCalibratedRed:0.180 green:0.188 blue:0.196 alpha:0.6],0.5,[NSColor colorWithCalibratedRed:0.137 green:0.137 blue:0.157 alpha:0.6],1.0, nil];
    [fill drawInBezierPath:path angle:-90.0];

    [[NSColor lightGrayColor] set];
    [path stroke];

Heres a picture of 2 of the corners (Its not as obvious in a small picture):

corners

Anyone know what's causing this? Am I just missing something?

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-11-09 08:27:43

圆角矩形的直线正好位于视图的边界上,因此每条线的宽度的一半被切断。 (就好像它们在子像素上一样。)

尝试更改

NSMakeRect(0, 0, [self bounds].size.width, [self bounds].size.height)

NSMakeRect(0.5, 0.5, [self bounds].size.width - 1, [self bounds].size.height - 1)

如果NSBezierPath看起来有点奇怪或模糊,请尝试将其移动半个像素。

The straight lines of the roundrect are exactly on the borders of the view, so half the width of each line is getting cut off. (As if they were on a subpixel.)

Try changing

NSMakeRect(0, 0, [self bounds].size.width, [self bounds].size.height)

to

NSMakeRect(0.5, 0.5, [self bounds].size.width - 1, [self bounds].size.height - 1)

If an NSBezierPath ever looks a bit weird or blurry, try shifting it over half a pixel.

相守太难 2024-11-09 08:27:43

查看 NSBezierPath 文档中的 setFlatness: 方法。它控制渲染曲线的平滑程度。我相信将其设置为较小的数字(默认为 0.6)将产生更平滑的曲线,但代价是更多的计算(尽管对于简单的路径,我怀疑这很重要)。

Take a look at the setFlatness: method in the NSBezierPath docs. It controls how smooth rendered curves are. I believe setting it to a smaller number (the default being .6) will yield smoother curves, at the cost of more computation (though for simple paths, I doubt it matters a whole lot).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文