我尝试用不同颜色绘制 UIBezierPath 线失败了。所有线条都变为当前选定的颜色。我所有的路径和信息都存储在一个名为 pathInfo 的 NSMutableArray 中。在路径信息中,我放入包含路径、颜色、宽度和线类型的数组。这很好用,除了所有的线条都变成用户选择的任何颜色。我将不胜感激任何帮助!
- (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;
}