有没有办法将 CGContextDrawRadialGradient 绘制为椭圆形而不是完美的圆形?
我需要一个椭圆形或椭圆形的径向渐变,看起来 CGContextDrawRadialGradient 只能绘制一个完美的圆形。我一直在绘制方形上下文,然后复制/绘制到矩形上下文中。
有更好的方法来做到这一点吗?
谢谢!
I need a radial gradient in the shape of an oval or ellipse and it seems like it CGContextDrawRadialGradient can only draw a perfect circle. I've been drawing to a square context then copying/drawing into a rectangular context.
Any better way to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现做到这一点的唯一方法是按照 Mark F 的建议,但我认为答案需要一个例子才能更容易理解。
在 iOS 的视图中绘制椭圆渐变(并使用 ARC):
放入带有黑色背景的视图中,您将得到:
The only way I've found to do this is as Mark F suggested, but I think the answer needs an example to be easier to understand.
Draw an elliptical gradient in a view in iOS (and using ARC):
Put in a view with a black background you get:
您可以更改上下文的变换来绘制椭圆(例如,在调用 CGContextDrawRadialGradient () 之前应用 CGContextScaleCTM(context, 2.0, 1.0) 来绘制宽度是高度两倍的椭圆渐变)。不过,请记住对起点和终点应用逆变换。
You can change the transform of the context to draw an ellipse (for example, apply CGContextScaleCTM(context, 2.0, 1.0) just before calling CGContextDrawRadialGradient () to draw an elliptical gradient that's twice as wide as it is high). Just remember to apply the inverse transform to your start and end points, though.