如何创建 UIBezierPath 渐变填充?
我想创建一个带有 10px 圆角和渐变填充的 UIBezierPath
。我怎样才能达到这个效果?
这是我想要做的事情的图像:
如您所见,这个正方形有:
- 2px 黑色边框
- 10px 圆角
- 红色到绿色线性渐变填充
如何在不使用图案图像颜色的情况下以编程方式执行此操作?
这是我创建路径的方法:
UIBezierPath *border = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:10.0f];
[border setLineWidth:2];
[[UIColor blackColor] setStroke];
[border stroke];
[[UIColor redColor] setFill]; <-- what should I put here?
[border fill];
I would like to create a UIBezierPath
with 10px rounded corners and with gradient fill. How can I acheive this effect?
Here's an image of what I want to do:
As you can see, this square has:
- 2px black border
- 10px rounded corners
- red to green linear gradient fill
How can I do this programatically without using pattern image color?
Here's how I create the path:
UIBezierPath *border = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:10.0f];
[border setLineWidth:2];
[[UIColor blackColor] setStroke];
[border stroke];
[[UIColor redColor] setFill]; <-- what should I put here?
[border fill];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的3种方法。
创建一个 CAGradientLayer 并将其作为 theView.layer 的子图层插入。您甚至可以在图层上放置圆角半径。当然,您必须导入 QuartzCore 框架。
使用 CoreGraphics 进行操作,如下所示:
创建一个一像素宽且具有渐变的离屏图像上下文,生成图像,然后使用 colorWithPatternImage 设置背景颜色。
这些按从最简单到最难的顺序排列。
3 ways that I can think of.
Create a CAGradientLayer and insert it as a sublayer of theView.layer. You can even put a rounded corner radius on the layer. Of course you'll have to import QuartzCore framework.
Do it with CoreGraphics, like so:
Create an off-screen image context that's one pixel wide and has a gradient, generate the image, then set the background color with colorWithPatternImage.
These are in order of easiest to hardest.