简体   繁体   中英

iOS - View background is black after adding multiple sublayers

I am creating a ring with multiple color segments. Here's my code:

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

I tried:

  1. setting the background color of the view self.backgroundColor = [UIColor clearColor];
  2. setting the background color of the layer self.layer.backgroundColor = [UIColor clearColor].CGColor
  3. setting the background color of the sublayers colorPieceLayer.backgroundColor = [UIColor clearColor].CGColor
  4. setting the opacity (But this will make the view disappear).

Here's my screenshot:

在此处输入图片说明

Here's my view layers:

在此处输入图片说明

So how am I going to make it clear color????

PLEASE TAKE NOTE:

The purple colored is just a UIView that holds my color ring view.

So when I change my view's background color , this color changed.

You could try it.

在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM