iOS - 添加多个子图层后查看背景为黑色

时间:2017-09-20 07:43:45

标签: ios objective-c cashapelayer

我正在创建一个包含多个颜色段的环。这是我的代码:

CGFloat viewWidth = rect.size.width;
CGFloat viewHeight = rect.size.height;
CGFloat lineWidth = viewWidth * 0.15f;
CGFloat radius = viewWidth/2 - lineWidth;

CGPoint center = CGPointMake(viewWidth/2, viewHeight/2);

UIColor *lavender = [UIColor colorWithRed:0.5f green:0.0f blue:1.0f alpha:1.0f];
UIColor *purple = [UIColor purpleColor];
UIColor *fuchsia = [UIColor colorWithRed:1.0 green:0.0f blue:0.5f alpha:1.0f];
UIColor *red = [UIColor redColor];
UIColor *orange = [UIColor orangeColor];
UIColor *yellow = [UIColor yellowColor];
UIColor *yellowGreen = [UIColor colorWithRed:0.5f green:1.0f blue:0.0f alpha:1.0f];
UIColor *green = [UIColor greenColor];
UIColor *blueGreen = [UIColor colorWithRed:0.0f green:1.0f blue:0.5f alpha:1.0f];
UIColor *cyan = [UIColor cyanColor];
UIColor *white = [UIColor whiteColor];
UIColor *lightBlue = [UIColor colorWithRed:0.0f green:0.5f blue:1.0f alpha:1.0f];
UIColor *blue = [UIColor blueColor];

NSArray *colors = @[lavender, purple, fuchsia,
                    red, orange, yellow,
                    yellowGreen, green, blueGreen,
                    cyan, white, lightBlue, blue];

for(int i = 0; i < 13; i++)
{
    CGFloat startAngle = 0.1538f * M_PI * i;
    CGFloat endAngle = 0.1538f * M_PI * (i + 1);

    UIColor *strokeColor = [colors objectAtIndex:i];

    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];

    CAShapeLayer *colorPieceLayer = [[CAShapeLayer alloc] init];
    [colorPieceLayer setPath:bezierPath.CGPath];
    [colorPieceLayer setStrokeColor:strokeColor.CGColor];
    [colorPieceLayer setFillColor:[UIColor clearColor].CGColor];
    [colorPieceLayer setLineWidth:lineWidth];

    [self.layer addSublayer:colorPieceLayer];
}

我试过了:

  1. 设置视图self.backgroundColor = [UIColor clearColor];
  2. 的背景颜色
  3. 设置图层self.layer.backgroundColor = [UIColor clearColor].CGColor
  4. 的背景颜色
  5. 设置子图层colorPieceLayer.backgroundColor = [UIColor clearColor].CGColor
  6. 的背景颜色
  7. 设置不透明度(但这会使视图消失)。
  8. 这是我的截图:

    enter image description here

    这是我的视图图层:

    enter image description here

    那么我怎样才能让它变得清晰?????

    请注意:

    紫色只是一个UIView,可以保持我的色环视图。

1 个答案:

答案 0 :(得分:0)

因此,当我更改视图的背景颜色时,此颜色会发生变化。

你可以尝试一下。

enter image description here

enter image description here