3

私はそのようなパスを描くカスタムUIViewを持っています

- (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には、塗りつぶしの色を変更するメソッドもあります

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

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

}

changeColor:メソッドを次のような事前定義された色のいずれかで呼び出すと

 [UIColor redColor]  

すべてが正常に動作します。代わりに、次のようなカスタムカラーを付けようとすると

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

カスタムUIViewの色は、白、赤、青の間でランダムにちらつきます。

それはなぜですか?

前もって感謝します!

4

2 に答える 2

6

試す:

CGContextSetFillColorWithColor(ctx, self.color.CGColor);

それ以外の:

CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
于 2012-02-29T15:52:12.487 に答える
0

このメソッド呼び出しを使用している場合:

[UIColor redColor];

最適化の目的で、UIColorは、他のコンポーネントを必要としないため、2つのコンポーネントのみを持つオブジェクトを返します。(これは、クラスクラスタリングのメカニズムで可能になります)。

これが、コードがこの2つの色の間にいくつかの違いを示している理由かもしれません。

于 2012-02-29T15:56:02.223 に答える