objective-c - UIBezierPath 多线条颜色

标签 objective-c nsmutablearray nsarray uibezierpath

我尝试用不同颜色绘制 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;

}

最佳答案

描边/填充颜色仅影响-描边命令。它们不会影响 -appendPath: 命令。路径不包含每段颜色信息。

如果您需要多色线条,则需要分别描边每种颜色。

关于objective-c - UIBezierPath 多线条颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36760600/

相关文章:

objective-c - 主线程中的 CHCSVParser

ios - UITableViewCell 标签不需要的缩进

iphone - 如何将字符串拆分为 NSMutableArray

ios - 如何在 Objective-C 、iOS 中将 NSMutableArray 数组分成两个单独的数组或两个可变数组

ios - 使用 NSPredicate 过滤 NSArray

ios - 使用 NSPredicate 在自定义对象的 NSArray 中搜索字符串

ios - 如何在不导致内存泄漏的情况下清除 CGBitMapContext?

objective-c - 为什么 IBAction 的发送者类型必须是 id?

ios - UIImageView 到 UITableViewCell

ios - (SKNode) 无法转换为字符串(它正在使用旧的 xCode)