UIBezierPath多种线条颜色 [英] UIBezierPath Multiple Line Colors

查看:640
本文介绍了UIBezierPath多种线条颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用不同的颜色绘制UIBezierPath线使我失败了.所有行将更改为当前选择的颜色.我所有的路径和信息都存储在一个名为pathInfo的NSMutableArray中.在路径信息中,我放入包含路径",颜色",宽度"和线型"的数组.除所有行变为用户选择的任何颜色外,此方法均正常运行.我将不胜感激!

My attempt to draw UIBezierPath lines with different colors is failing me. All the lines change to the currently selected color. All my path and information are all stored in an NSMutableArray called pathInfo. In path info I drop in array that contains the Path, Color, Width, and Type of line. This works fine except all the lines turn to whatever color the user has selected. I would appreciate any help greatly!

- (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;

}

推荐答案

笔触/填充颜色仅影响-stroke命令.它们不影响-appendPath:命令.路径不包含每个细分的颜色信息.

The stroke/fill colors only affect the -stroke command. They don't affect the -appendPath: command. Paths don't contain per-segment color information.

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

If you need a multi-colored line you're going to need to stroke each color separately.

这篇关于UIBezierPath多种线条颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆