2009-08-19 49 views
1

我有许多'line'对象 - 每个对象存储关于使用Core Graphics绘制的线条的信息。问题是,虽然可能有多个线对象,每个线对象都具有唯一的颜色和笔触宽度,但所有线都在SAME颜色和笔触宽度下绘制。试图为每个笔画设置独特的CoreGraphics(iphone)状态

每一行对象都具有诸如笔触颜色,笔画宽度和CGPoint的NSMutableArray等属性。在我的drawRect方法中,我有一个NSEnumerator迭代器,它处理每个线对象的每个CGPoint以及一个while循环,它可以将上面的对象集合到每一行中。在每条新线的开始处,我使用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

的线条,只有当你调用CGContextStrokePath真正拿得出,所以所有的线都得到绘制您添加的最后一行的颜色和宽度。 我认为,如果你只是移动while循环中的

CGContextStrokePath(myContext); 

线,你会得到你想要的行为。 CGContextStrokePath也会在绘图后清除当前路径,所以它应该没问题。

相关问题