如何在UIView中绘制多个不同颜色的UIBezierPath
我想在 UIView 中绘制多个具有不同描边和填充颜色的 UIBezierPath。
这是代码
- (void)drawRect:(CGRect)rect {
context = UIGraphicsGetCurrentContext();
[[UIColor grayColor] setFill];
[[UIColor greenColor] setStroke];
UIBezierPath *aPath = [[UIBezierPath alloc] init];
[aPath moveToPoint:CGPointMake(227,34.25)];
[aPath addLineToPoint:CGPointMake(298.25,34.75)];
[aPath addLineToPoint:CGPointMake(298.5,82.5)];
[aPath addLineToPoint:CGPointMake(251,83)];
[aPath addLineToPoint:CGPointMake(251,67.5)];
[aPath addLineToPoint:CGPointMake(227.25,66.75)];
[aPath closePath];
aPath.lineWidth = 2;
[aPath fill];
[aPath stroke];
UIBezierPath *aPath2 = [[UIBezierPath alloc] init];
[aPath2 moveToPoint:CGPointMake(251.25,90.5)];
[aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
[aPath2 addLineToPoint:CGPointMake(298.5,83)];
[aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
[aPath2 closePath];
aPath2.lineWidth = 2;
[aPath2 fill];
[aPath2 stroke];
[paths addObject:aPath2];
问题是描边和填充颜色是在当前上下文中设置的。 是否可以在同一个CGContextRef中绘制不同颜色的不同UIBezierPath?
或者我必须在单独的 UIView 中绘制每个 UIBezierPath ?
I would like to draw multiple UIBezierPath in a UIView with different stroke and fill color.
Here is the code
- (void)drawRect:(CGRect)rect {
context = UIGraphicsGetCurrentContext();
[[UIColor grayColor] setFill];
[[UIColor greenColor] setStroke];
UIBezierPath *aPath = [[UIBezierPath alloc] init];
[aPath moveToPoint:CGPointMake(227,34.25)];
[aPath addLineToPoint:CGPointMake(298.25,34.75)];
[aPath addLineToPoint:CGPointMake(298.5,82.5)];
[aPath addLineToPoint:CGPointMake(251,83)];
[aPath addLineToPoint:CGPointMake(251,67.5)];
[aPath addLineToPoint:CGPointMake(227.25,66.75)];
[aPath closePath];
aPath.lineWidth = 2;
[aPath fill];
[aPath stroke];
UIBezierPath *aPath2 = [[UIBezierPath alloc] init];
[aPath2 moveToPoint:CGPointMake(251.25,90.5)];
[aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
[aPath2 addLineToPoint:CGPointMake(298.5,83)];
[aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
[aPath2 closePath];
aPath2.lineWidth = 2;
[aPath2 fill];
[aPath2 stroke];
[paths addObject:aPath2];
The problem is that the stroke and the fill color are set on the current context.
Is it possible to draw different UIBezierPath with different colors in the same CGContextRef?
Or I have to draw each UIBezierPath in separate UIView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该添加
指示这些是必须在此上下文中进一步使用的新颜色。您应该在每次调用 a 之前执行此操作,
以便用这些颜色绘制路径。
无需为每个贝塞尔曲线路径使用新视图。
You should add
indicating that these are the new colors that have to used further on in this context. You should do this before every time you call a
so that the paths be drawn with those colors.
No need to use a new view for each bezier path.
使用此
In touch 开始将您的路径和颜色存储在两个数组中,并像上面一样在 drawrect 中使用它们。
use this
In touch began store your paths and colors in two arrays and use them in drawrect like above.
只需定义一个 UIColor *setStroke;在 .h 文件中,并在调用
[myPathStrokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 之前设置此描边颜色对象;
Just define a UIColor *setStroke; in .h file and set this strokeColor object before you call
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];