Question

I have a custom UIView which draws a path like so

- (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];

}

My custom UIView also has a method to change the filling color

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

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

}

If I call the changeColor: method with any of the predefined colors such as

 [UIColor redColor]  

everything works fine. Instead if i try to give it a custom color such as

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

the color of my custom UIView flicker between white, red and blue randomly.

Any ideas of why is that?

Thank you in advance!

Was it helpful?

Solution

Try:

CGContextSetFillColorWithColor(ctx, self.color.CGColor);

instead of:

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

OTHER TIPS

When you are using this method call :

[UIColor redColor];

For optimization purposes, UIColor return an object with only 2 components because he don't need the other one. (This is made possible with the mechanism of Class clustering).

That's maybe why your code show some differences between this two colors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top