ios 如何在 UIView 中绘制多个不同颜色的 UIBezierPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6431005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw multiple UIBezierPath with different colors in a UIView
提问by iMacX
I would like to draw multiple UIBezierPath in a UIView with different stroke and fill color.
我想在具有不同笔触和填充颜色的 UIView 中绘制多个 UIBezierPath。
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?
问题是笔触和填充颜色是在当前上下文中设置的。是否可以在同一个 CGContextRef 中用不同的颜色绘制不同的 UIBezierPath?
Or I have to draw each UIBezierPath in separate UIView?
或者我必须在单独的 UIView 中绘制每个 UIBezierPath?
回答by Deepak Danduprolu
You should add
你应该添加
[desiredStrokeColor setStroke];
[desiredFillColor setFill];
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
表明这些是在这种情况下必须进一步使用的新颜色。你应该在每次打电话之前这样做
[aNewPath fill];
[aNewPath stroke];
so that the paths be drawn with those colors.
以便使用这些颜色绘制路径。
No need to use a new view for each bezier path.
无需为每个贝塞尔曲线路径使用新视图。
回答by Veera Raj
use this
用这个
int count = 0;
for(UIBezierpath *_paths in pathArray)
{
UIColor *_color = [delegate1.colorArray objectAtIndex:q];
[_color setStroke];
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
count++;
}
In touch began store your paths and colors in two arrays and use them in drawrect like above.
In touch 开始将您的路径和颜色存储在两个数组中,并像上面一样在 drawrect 中使用它们。
回答by Rakesh
Just define a UIColor *setStroke; in .h file and set this strokeColor object before you call [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
只需定义一个 UIColor *setStroke; 在 .h 文件中并在调用之前设置此 strokeColor 对象[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
- (void)drawRect:(CGRect)rect
{
[strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
for(UIBezierPath *_path in pathArray)
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth = currentSliderValue;
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
[pathArray addObject:myPath];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}