尝试为每个笔划设置唯一的CoreGraphics(iphone)状态

时间:2009-08-19 10:57:22

标签: iphone uikit core-graphics graphicscontext

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

每个线对象都有诸如笔触颜色,笔触宽度和CGPoints的NSMutableArray之类的属性。在我的drawRect方法中,我有一个NSEnumerator迭代器,它处理每个行对象的每个CGPoint,以及一个while循环,它对我的​​集合中的每一行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");

    }
}

1 个答案:

答案 0 :(得分:1)

当您调用CGContextStrokePath时,实际上只会绘制线条,因此所有线条都会使用您添加的最后一行的颜色和宽度绘制。 我想如果你只是移动

CGContextStrokePath(myContext);
在while循环中输入

行,您将获得所需的行为。 CGContextStrokePath也会在绘制后清除当前路径,因此应该没问题。