我尝试用不同的颜色绘制UIBezierPath线条的尝试失败了。所有线条都将更改为当前选定的颜色。我的所有路径和信息都存储在一个名为pathInfo的NSMutableArray中。在Path info中,我放入包含path、Color、Width和Type of line的数组中。这工作得很好,除了所有的线条都会变成用户选择的任何颜色。我将非常感谢任何人的帮助!
- (void)drawRect:(CGRect)rect {
UIBezierPath *drawPath = [UIBezierPath bezierPath];
drawPath.lineCapStyle = kCGLineCapRound;
drawPath.miterLimit = 0;
for (int i = 0; i < [pathInfo count]; i++){
NSArray *row = [[NSArray alloc] initWithArray:[pathInfo objectAtIndex:i]];
NSLog(@"Path: %@",[row objectAtIndex:0]);
NSLog(@"Color: %@",[row objectAtIndex:1]);
NSLog(@"Width: %@",[row objectAtIndex:2]);
NSLog(@"Type: %@",[row objectAtIndex:3]);
//width
drawPath.lineWidth = [[row objectAtIndex:2] floatValue];
//color
[[row objectAtIndex:1] setStroke];
//path
[drawPath appendPath:[row objectAtIndex:0]];
}
UIBezierPath *path = [self pathForCurrentLine];
if (path)
[drawPath appendPath:path];
[drawPath stroke];
}
- (UIBezierPath*)pathForCurrentLine {
if (CGPointEqualToPoint(startPoint, CGPointZero) && CGPointEqualToPoint(endPoint, CGPointZero)){
return nil;
}
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
return path;
}发布于 2013-02-16 05:35:16
描边/填充颜色仅影响-stroke命令。它们不会影响-appendPath:命令。路径不包含每个分段的颜色信息。
如果你需要一条多色的线条,你需要分别描画每种颜色。
发布于 2013-02-16 06:32:39
设置您的笔触颜色(以及您拥有的颜色),然后设置stroke,然后移动到下一路径:
- (void)drawRect:(CGRect)rect
{
for (int i = 0; i < [pathInfo count]; i++){
NSArray *row = [[NSArray alloc] initWithArray:[pathInfo objectAtIndex:i]];
NSLog(@"Path: %@",[row objectAtIndex:0]);
NSLog(@"Color: %@",[row objectAtIndex:1]);
NSLog(@"Width: %@",[row objectAtIndex:2]);
NSLog(@"Type: %@",[row objectAtIndex:3]);
UIBezierPath *path = [row objectAtIndex:0];
path.lineCapStyle = kCGLineCapRound;
path.miterLimit = 0;
//width
path.lineWidth = [[row objectAtIndex:2] floatValue];
//color
[[row objectAtIndex:1] setStroke];
//path
[path stroke];
}
UIBezierPath *path = [self pathForCurrentLine];
if (path)
{
// set the width, color, etc, too, if you want
[path stroke];
}
}https://stackoverflow.com/questions/14903572
复制相似问题