CAShapeLayer fillColor无法渲染

时间:2014-03-13 08:31:45

标签: ios objective-c cocoa-touch cashapelayer

我有一个方法接受UIBezierPath作为参数并相应地绘制一个形状。如果我用纯色填充形状它会渲染得很好但是如果我尝试使它成为带alpha的颜色,我首先得到一个ARC警告,要求更改代码并使用桥接,然后填充颜色根本不渲染。

这有效,但不是我想要的:

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

这会生成一个ARC警告"将Objective C指针类型UIColor隐式转换为C指针CGColorRef"。

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

这会运行,但不会对形状应用fillColor:

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

以下是该方法的完整代码:

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

}

如果允许使用alpha填充CAShapeLayer形状,我需要做些什么?

2 个答案:

答案 0 :(得分:2)

您的alpha不正确。

  

alpha - 颜色对象的不透明度值,指定为值   从0.0到1.0。

尝试更改

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

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

答案 1 :(得分:0)

虽然我认为我写的是正确的,但显然不是。

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

应该是:

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