我有许多“线”对象——每个对象都存储有关使用 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");
}
}