1

我有许多“线”对象——每个对象都存储有关使用 Core Graphics 绘制的线的信息。问题是,尽管可能有多个线条对象,每个对象都具有唯一的颜色和笔划宽度,但所有线条都在相同的颜色和笔划宽度下绘制。

每个线条对象都具有诸如描边颜色、描边宽度和 CGPoints 的 NSMutableArray 等属性。在我的 drawRect 方法中,我有一个 NSEnumerator 迭代器,它遍历每个 line 对象的每个 CGPoint,还有一个 while 循环,它对我的​​集合中的每个 line Object 执行上述操作。在每条新行的开头,我使用 CGContext 方法(下面的代码)设置笔触颜色和粗细。如何用自己独特的颜色绘制每条线?

- (void)drawRect:(CGRect)rect {

    if(hasDrawnBefore){


        myContext = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(myContext, kCGLineCapRound);

        int numberOfLines = [myLines count];
        int h = 0;
        while(h < numberOfLines){

            CGContextSetStrokeColorWithColor(myContext, [[[myLines objectAtIndex:h] lineColor] CGColor]);
            CGContextSetLineWidth(myContext, [[myLines objectAtIndex:h] lineThickness]);
            NSLog(@"<---------------------- Changed Color and Stroke-Width! ------------------------>");


            NSEnumerator *myEnumerator = [[[myLines objectAtIndex:h] linePoints] objectEnumerator];
            PointObject* object;
            int enumIndex = 0;

            while ((object = [myEnumerator nextObject])) {


                if(enumIndex == 0){

                    CGContextMoveToPoint(myContext, object.coordX,  object.coordY);

                }else{


                    CGContextAddLineToPoint(myContext, object.coordX, object.coordY);

                }

                enumIndex++;
            }

            NSLog(@"Just Iterated on the %i th line of %i lines", h, numberOfLines);
            h++;    
        }

        CGContextStrokePath(myContext);
        NSLog(@"DRAWING");

    }else{
        NSLog(@"No Drawings Yet");

    }
}
4

1 回答 1

1

这些线条只有在您调用 CGContextStrokePath 时才会真正绘制,因此您的所有线条都使用您添加的最后一条线条的颜色和宽度来绘制。我想如果你只是移动

CGContextStrokePath(myContext);

在while循环内,你会得到你想要的行为。CGContextStrokePath 绘制后也会清除当前路径,所以应该没问题。

于 2009-08-20T03:41:52.357 回答