NSBezierPath图 [英] NSBezierPath drawing

查看:468
本文介绍了NSBezierPath图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NSImage上做一个圆角矩形轮廓,我想使用NSBezierPath将是最好的方法。但是,我遇到了一个问题:我没有画出一个好的曲线,我得到这个:

I want to do a rounded rectangle outline on an NSImage and I figured that using NSBezierPath would be the best way. However, I ran into a problem: instead of drawing a nice curve, I get this:

由于我不明白的原因,NSBezierPath绘制圆形部分,

For reasons I can't understand, NSBezierPath is drawing the rounded part with a darker color than the rest.

这是我使用的代码(在自定义视图中的drawRect:调用中):

Here's the code I'm using (inside a drawRect: call on a custom view):

NSBezierPath* bp = [NSBezierPath bezierPathWithRoundedRect: self.bounds xRadius: 5 yRadius: 5];
[[[NSColor blackColor] colorWithAlphaComponent: 0.5] setStroke];
[bp stroke];

任何想法?

如果我把路径插入0.5,一切都很好。但是为什么当我将路径偏移10像素(例如)时会得到这个?

If I inset the path by 0.5 everything draws just fine. But why is it that I get this when I offset the path by 10 pixels (for example)?

如果我理解正确,它也应该画一条细线...

If I understand correctly, it should draw a thin line as well...

推荐答案

许多渲染系统派生自PostScript绘图模型。核心图形是这些衍生系统之一。 (以下是其他一些:PDF, SVG HTML Canvas 2D上下文 Cairo 。)

Many rendering systems are derived from the PostScript drawing model. Core Graphics is one of these derivative systems. (Here are some others: PDF, SVG, the HTML Canvas 2D Context, Cairo.)

所有这些系统都有用一个固定宽度的线条敲击路径的想法。当您描绘路径时,线跨越的路径:线的宽度的一半在路径的一侧,并且线的宽度的一半在另一侧。下面是一个图表,可以使这更清楚:

All of these systems have the idea of stroking a path with a line of some fixed width. When you stroke the path, the line straddles the path: half of the line's width is on one side of the path, and half of the line's width is on the other side. Here's a diagram that may make this clearer:

现在,当您沿着视图边界划一条路径时会发生什么?一半的笔画将落在视图的边界之外,并被剪掉 - 而不是绘制。

Now, what happens when you stroke a path that lies along the boundary of your view? Half of the stroke will fall outside of your view's bounds and be clipped away - not drawn. You will only see the half of the stroke that falls inside the view's bounds.

当您使用圆角时,该角会从视图的边界向其中心移动,所以更多的角落在视角的边界内。因此,笔划在圆角周围变得更加粗糙,如下所示:

When you use a rounded corner, that corner pulls away from the view's boundary, toward its center, so more of the stroke around the corner falls inside the view's boundary. So the stroke appears to get thicker around the rounded corner, like this:

为了解决这个问题,你需要将你的路径插入一半的线宽,以便整个笔触都在视图的边界沿着整个路径。默认行宽为1.0,因此:

To fix this, you need to inset your path by half the line width, so that the entire stroke falls inside your view's bounds along the entire path. The default line width is 1.0, so:

NSBezierPath* bp = [NSBezierPath bezierPathWithRoundedRect:
    NSRectInset(self.bounds, 0.5, 0.5) xRadius:5 yRadius:5];

这篇关于NSBezierPath图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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