2012-02-29 10 views
3

私のようなパスを描画するカスタムUIViewのを持っているので、UIColorとグラフィックスコンテキスト

- (void)drawRect:(CGRect)rect{ 

    [[UIColor blackColor] setStroke]; 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColor(ctx, CGColorGetComponents(self.color)); 

    CGContextFillPath(ctx); 

    UIBezierPath *thePath = [UIBezierPath bezierPath]; 
    thePath.lineWidth = _lineWidth; 

    _center = CGPointMake(rect.size.width, rect.size.height); 

    [thePath moveToPoint:_center]; 

    [thePath addArcWithCenter:_center radius:rect.size.width startAngle:M_PI endAngle:M_PI+degreesToRadians(_angle)clockwise:YES]; 

    [thePath closePath]; 
    [thePath fill]; 

} 

マイカスタムUIViewのも、私はchangeColorを呼び出す場合、充填色

- (void) changeColor:(UIColor *) newColor { 

    self.color = [newColor CGColor]; 
    [self setNeedsDisplay]; 

} 

を変更する方法があります。方法は、

[UIColor redColor] 

のような定義済みの色のいずれかを使用します。代わりに、私はそれをランダムに、白、赤と青の間の私のカスタムUIViewのちらつきの色を、そのような

UIColor* color = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f]; 

などのカスタムカラーを与えることをしようとした場合。

それはなぜですか?

ありがとうございます!

答えて

6

試してみてください。

CGContextSetFillColorWithColor(ctx, self.color.CGColor); 

の代わり:

CGContextSetFillColor(ctx, CGColorGetComponents(self.color)); 
+0

うん、そうだった。厄介な小さなこと。 [The Documentation](https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CGContext/Reference/reference.html#//apple_ref/doc/uid/TP30000950-CH1g)のドキュメントに記載されているとおり-BCIJCGGJ)**使用する優先APIがCGContextSetFillColorWithColorになりました。** – tmpz

+0

ありがとうございました:) – tmpz

0

あなたは、このメソッドの呼び出しを使用している:最適化の目的のために

[UIColor redColor]; 

は、UIColorが唯一の2つのコンポーネントを持つオブジェクトを返します彼はもう片方が必要ないからです。 (これはクラスクラスタリングのメカニズムで可能になります)。

あなたのコードでは、この2つの色の間にいくつかの違いがあるのか​​もしれません。

関連する問題