我正在创建一个包含多个颜色段的环。这是我的代码:
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];
}
我试过了: