CGContextDrawAngleGradient?
我将脚浸入更多的 Core Graphics 绘图中,试图重新创建一个看起来很邪恶的金属旋钮,并且我发现了一个可能是一个令人停止的问题。
似乎没有任何方法可以在 Core Graphics 中绘制角度渐变。我看到有 CGContextDrawRadialGradient()
和 CGContextDrawLinearGradient()
,但我没有看到任何可以让我绘制角度渐变的东西。有谁知道解决方法,或者隐藏在某处的一些框架来完成此操作,而无需将旋钮预先渲染到图像文件中?
AngleGradientKnob http://dl.dropbox.com/u/3009808/AngleGradient.png。
Dipping my feet into some more Core Graphics drawing, I'm attempting to recreate a wicked looking metallic knob, and I've landed on what is probably a show-stopping issue.
There doesn't seem to be any way to draw angle gradients in Core Graphics. I see there's CGContextDrawRadialGradient()
and CGContextDrawLinearGradient()
, but there's nothing that I see that would allow me to draw an angle gradient. Does anyone know of a workaround, or a bit of framework hidden away somewhere to accomplish this without pre-rendering the knob into an image file?
AngleGradientKnob http://dl.dropbox.com/u/3009808/AngleGradient.png.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种拼凑在一起的方式,但这是我可能会采取的方法。这是通过使用一些简单的三角函数将其直接绘制到位图中来创建角度渐变,然后将其剪切为圆形。我使用灰度色彩空间创建内存网格,计算从给定点到中心的角度,然后基于周期函数进行着色,在 0 到 255 之间运行。当然,您也可以扩展它以执行 RGBA 颜色。
当然,您可以缓存它并进行数学运算以获得您想要的颜色。目前它是从黑到白的,看起来并不像你想要的那么好。
This is kind of thrown together, but it's the approach I'd probably take. This is creating an angle gradient by drawing it directly into a bitmap using some simple trig, then clipping it to a circle. I create a grid of memory using a grayscale colorspace, calculate the angle from a given point to the center, and then color that based on a periodic function, running between 0 to 255. You could of course expand this to do RGBA color as well.
Of course you'd cache this and play with the math to get the colors you want. This currently runs all the way from black to white, which doesn't look as good as you'd like.
为了扩展已接受答案的注释中的内容,这里是使用 Core Image 生成角度渐变的代码。这应该适用于 iOS 8 或更高版本。
这会生成以下图像:
To expand on what's in the comments to the accepted answer, here's the code for generating an angle gradient using Core Image. This should work in iOS 8 or later.
This generates the following image: