Question

I have a method that accepts a UIBezierPath as a parameter and draws a shape accordingly. If I fill the shape with a solid colour it renders fine but if I try to make it a colour with alpha, I first get an ARC warning asking to change the code and use a bridge and then the fill colour does not render at all.

This works, but is not what I want:

standShapeLayer.fillColor = [[UIColor redColor] CGColor];

This generates an ARC warning "implicit conversion of Objective C pointer type UIColor to C pointer CGColorRef".

standShapeLayer.fillColor = [UIColor colorWithRed:0.2 green:0.66 blue:0.86 alpha:2.0f];

This runs but no fillColor is applied to the shape:

standShapeLayer.fillColor = (__bridge CGColorRef)([UIColor colorWithRed:0.2 green:0.66 blue:0.86 alpha:2.0f]);

Here is the whole code for the method:

-(void)drawShape:(UIBezierPath *)standShape {

    [standShapeLayer removeFromSuperlayer];
    selectedStand = standShape;
    standShapeLayer = [CAShapeLayer layer];
    standShapeLayer.path = standShape.CGPath;
    standShapeLayer.fillColor = (__bridge CGColorRef)([UIColor colorWithRed:0.2 green:0.66 blue:0.86 alpha:2.0f]);
    NSLog(@"color = %@", standShapeLayer.fillColor); // This logs the correct colour which is not rendered.
    // standShapeLayer.fillColor = [[UIColor redColor] CGColor]; << This works but is not what I need.
    standShapeLayer.strokeColor = [[UIColor redColor] CGColor];
    standShapeLayer.lineWidth = 2;
    [_imageView.layer addSublayer:standShapeLayer];

}

What do I need to do to allow colours with alpha to fill CAShapeLayer shapes?

Was it helpful?

Solution 2

Although what I had written I thought was correct, it clearly wasn't.

standShapeLayer.fillColor = [UIColor colorWithRed:0.2 green:0.66 blue:0.86 alpha:2.0f];

Should be:

[standShapeLayer setFillColor:[[UIColor colorWithRed:0.2 green:0.66 blue:0.86 alpha:2.0f] CGColor]];

OTHER TIPS

Your alpha is incorrect.

alpha - The opacity value of the color object, specified as a value from 0.0 to 1.0.

Try to change

standShapeLayer.fillColor = (__bridge CGColorRef)([UIColor colorWithRed:0.2 green:0.66 blue:0.86 alpha:2.0f]);

to

standShapeLayer.fillColor = [[UIColor colorWithRed:0.2 green:0.66 blue:0.86 alpha:0.7f] CGColor];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top