views:

268

answers:

1

I've got a number of 'line' objects -- each storing information about a line drawn using Core Graphics. The problem is, that though there may be several line objects each having a unique color and stroke width, all lines are getting drawn under the SAME color and stroke width.

Each line object has attributes such as stroke color, stroke width, and an NSMutableArray of CGPoints. In my drawRect method, I have an NSEnumerator iterator working through each CGPoint of each line object and a while loop that does the above to each line Object in my collection. At the start of each new line I set the stroke color and thickness using CGContext methods (code below). How can I draw each line with its own unique color?

- (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  A: 

The lines only actually get drawn when you call CGContextStrokePath, so all your lines are getting drawn with the color and width of the last line you added. I think if you just move the

CGContextStrokePath(myContext);

line inside the while loop, you will get your desired behavior. CGContextStrokePath also clears the current path after drawing, so it should be fine.

kiyoshi