2009-08-19 50 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也會在繪圖後清除當前路徑,所以它應該沒問題。

相關問題