【问题标题】:UIBezierPath Multiple Line ColorsUIBezierPath 多行颜色
【发布时间】:2013-02-15 21:25:52
【问题描述】:

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

}

【问题讨论】:

  • 如果可能,请将您的完整代码传递给我。其实我也面临同样的问题。但我使用的是 touchesBegan、touchesMoved 和 touchesEnd 方法。我做不到。

标签: objective-c nsmutablearray nsarray uibezierpath


【解决方案1】:

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

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

【讨论】:

  • 我该怎么做@Kevin-Ballard
  • @JasonBourne:不要将所有路径附加到一个主路径中,只需依次描边每条路径即可。
  • 这不是我在做的吗?每个人都在 for 声明中被绘制@kevin-ballard
  • @JasonBourne:不,他们没有。每个都附加到您的drawPath 路径,然后在最后您抚摸drawPath
  • @JasonBourne:我不知道你的 -pathForCurrentLine 方法中有什么,但肯定是在 for 循环中,而不是说 [drawPath appendPath:[row objectAtIndex:0]] 你应该说 [[row objectAtIndex:0] stroke]
【解决方案2】:

设置你的描边颜色(以及你有什么),然后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];
    }
}

【讨论】:

  • 哇,你的回答也有效!我也感谢你的帮助,罗布!这种方法似乎效果更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多