使用 Quartz 2D / Core Graphics 完成此绘图的最佳方法是什么?
作为我的应用程序中视图之一的背景,我想在其框架内绘制一个相当简单的矩形边框。 这本质上是一个矩形渐变:框架周围的黑线,大约 10-20 像素处渐变为白色。不幸的是,据我所知,Core Graphics 不提供矩形渐变(无论是使用 CGGradient< /code> 或 CGShading
)。 所以我想知道最好的方法是什么。
我想到的有两个:
- 绘制一系列同心矩形,每个后续矩形的颜色较浅,并在每边插入 1px。 我想不出更简单的方法,但我必须自己完成所有梯度计算,并且可能需要大量图形操作。
- 在线性模式下使用
CGGradient
,每边一次。 但为了实现这一点,我想我需要首先为每一侧设置一个梯形剪切区域,以便渐变在拐角处斜接。
似乎应该有一种方法可以使用路径描边来做到这一点,但似乎没有一种方法可以定义两侧方向不同的模式。
As the background for one of the views in my app, I'd like to draw a fairly simple rectangular border just inside its frame. This would essentially be a rectangular gradient: a black line around the frame, fading to white about 10-20 pixels in. Unfortunately, as far as I can tell, Core Graphics doesn't provide rectangular gradients (either with CGGradient
or CGShading
). So I'm wondering what the best approach would be.
Two that occur to me:
- Draw a series of concentric rectangles, each subsequent one lighter in color, and inset by 1px on each side. I can't think of a simpler approach, but I have to do all of the gradient calculations myself, and it might be a lot of graphics operations.
- Use
CGGradient
in linear mode, once for each side. But for this to work, I think I'd need to set up a trapezoidal clipping area for each side first, so that the gradients would be mitered at the corners.
Seems like there should be a way to use path stroking to do this, but it doesn't seem like there's a way to define a pattern that's oriented differently on each side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会选择选项#2:
使用 NSBezierPath 创建梯形区域相当简单,您只需执行四次绘制操作。
以下是创建左侧梯形区域的基本代码:
I would go with option #2:
Using
NSBezierPath
to create the trapezoidal regions would be fairly straightforward, and you would only have to perform four drawing operations.Here's the basic code for creating the left side trapezoidal region:
像这样的东西也可以工作。
基本上:不使用剪切路径,只需使用混合模式。
在此示例中,渐变被缓存在 CGLayer 中。
something like this could also work.
basically: instead of using clipping paths, simply use blendmode.
and in this example the gradient is cached in a CGLayer.