2010-10-27 76 views
0

在一些代碼段核心圖形狀態管理

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 
    CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]); 

    UIGraphicsPushContext(ctx); 
    ... 

當前填充顏色被設置,那麼狀態被壓入堆棧。 其他片段:

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 
    UIGraphicsPushContext(ctx); 

    [[UIColor darkTextColor] set]; 
    ... 

哪種方式正確?在狀態管理方面,這兩種方法CGContextSetFillColorWithColorUIColor set之間有什麼區別?

回答

1

唯一的區別是,當您使用UIColor方法時,您不能指定更新哪個上下文,它總是更新當前上下文。 UIGraphicsPushContext將新的上下文設置爲當前的上下文,所以當然這個順序很重要。 (另一個小的區別是set方法設置填充顏色和筆觸顏色)。

否則沒有區別,你可以使用任何你喜歡的。

在你的例子中,我可能會使用CGContextSetFillColorWithColor,因爲在這種情況下,你不必使用UIGraphicsPushContext方法。