画一个圆圈,用不同的颜色填充不同的部分

时间:2013-06-24 06:53:59

标签: ios ipad core-graphics quartz-2d

我是一名新手ios程序员。在我的一个项目中,我需要绘制一个圆圈,其中圆圈的不同部分将填充不同的颜色。我可以绘制圆圈。但我无法确定圆圈的不同部分,并用不同的颜色填充它们。这是一个截图,以阐明我想要绘制的内容。circle with different color

将不胜感激。

3 个答案:

答案 0 :(得分:11)

您可以使用UIBezierPath方法addArcWithCenter:radius:startAngle:endAngle:clockwise:,您可以在其中指定半径,中心和角度。代码看起来像这样,以绿色绘制四分之一圆:

CGPoint center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f, CGRectGetHeight(self.bounds) / 2.f);
CGFloat radius = center.x - 10.f;

UIBezierPath *portionPath = [UIBezierPath bezierPath];
[portionPath moveToPoint:center];
[portionPath addArcWithCenter:center radius:radius startAngle:0.f endAngle:M_PI_2 clockwise:YES];
[portionPath closePath];

[[UIColor greenColor] setFill];
[portionPath fill];

UIBezierPath *portionPath1 = [UIBezierPath bezierPath];
[portionPath1 moveToPoint:center];
[portionPath1 addArcWithCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
[portionPath1 closePath];

[[UIColor blueColor] setFill];
[portionPath1 fill];

当然,您也可以考虑使用像CorePlot这样的库。

答案 1 :(得分:3)

我们有6个班级

CircleViewController.h,CircleViewController.m,GraphView.h,GraphView.h,CirclePart.h,CirclePart.m

<强> CirclePart.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CirclePart : NSObject

@property (nonatomic) CGFloat startDegree;
@property (nonatomic) CGFloat endDegree;
@property (nonatomic) UIColor *partColor;

-(id)initWithStartDegree:(CGFloat)startDegree endDegree:(CGFloat)endDegree partColor:(UIColor*)partColor;

@end

<强> CirclePart.m

#import "CirclePart.h"

@implementation CirclePart

-(id)initWithStartDegree:(CGFloat)startDegree endDegree:(CGFloat)endDegree partColor:(UIColor*)partColor
{
    self = [super init];
    if (self) {
        self.startDegree = startDegree;
        self.endDegree = endDegree;
        self.partColor = partColor;
    }
    return self;
}

@end

<强> GraphView.h

#import <UIKit/UIKit.h>
#import "CirclePart.h"

@interface GraphView : UIView

@property (nonatomic) CGPoint centrePoint;
@property (nonatomic) CGFloat radius;
@property (nonatomic) CGFloat lineWidth;
@property (nonatomic, strong) NSArray *circleParts;

-(id)initWithFrame:(CGRect)frame CentrePoint:(CGPoint)centrePoint radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth circleParts:(NSArray*)circleParts;

@end

<强> GraphView.m

#import "GraphView.h"

@implementation GraphView

-(id)initWithFrame:(CGRect)frame CentrePoint:(CGPoint)centrePoint radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth circleParts:(NSArray*)circleParts
{
    self = [super initWithFrame:frame];
    if (self) {
        self.centrePoint = centrePoint;
        self.radius = radius;
        self.lineWidth = lineWidth;
        self.circleParts = circleParts;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    [self drawCircle];
}

- (void)drawCircle {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, _lineWidth);

    for(CirclePart *circlePart in _circleParts)
    {
        CGContextMoveToPoint(context, _centrePoint.x, _centrePoint.y);

        CGContextAddArc(context, _centrePoint.x , _centrePoint.y, _radius, [self radians:circlePart.startDegree], [self radians:circlePart.endDegree], 0);
        CGContextSetFillColorWithColor(context, circlePart.partColor.CGColor);
        CGContextFillPath(context);
    }
}

-(float) radians:(double) degrees {
    return degrees * M_PI / 180;
}


@end

<强> CircleViewController.h

#import <UIKit/UIKit.h>

@interface CircleViewController : UIViewController

@end

<强> CircleViewController.m

#import "CircleViewController.h"
#import "GraphView.h"
#import "CirclePart.h"

@interface CircleViewController ()

@end

@implementation CircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CirclePart *part1 = [[CirclePart alloc] initWithStartDegree:0 endDegree:120 partColor:[UIColor redColor]];
    CirclePart *part2 = [[CirclePart alloc] initWithStartDegree:120 endDegree:250 partColor:[UIColor blueColor]];
    CirclePart *part3 = [[CirclePart alloc] initWithStartDegree:250 endDegree:360 partColor:[UIColor grayColor]];

    NSArray *circleParts = [[NSArray alloc] initWithObjects:part1, part2, part3, nil];

    CGRect rect = CGRectMake(100, 100, 200, 200);
    CGPoint circleCenter = CGPointMake(rect.size.width / 2, rect.size.height / 2);

    GraphView *graphView = [[GraphView alloc] initWithFrame:rect CentrePoint:circleCenter radius:80 lineWidth:2 circleParts:circleParts];
    graphView.backgroundColor = [UIColor whiteColor];
    graphView.layer.borderColor = [UIColor redColor].CGColor;
    graphView.layer.borderWidth = 1.0f;

    [self.view addSubview:graphView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

答案 2 :(得分:0)

这是一个简单的三角学问题。计算饼图部分所需的角度(饼图切片百分比时间360度),然后移动到中心。以适当的角度划线到圆形边缘,以圆弧角度到达饼形切片的下一侧,然后返回到中心。

或者,您可以使用CorePlot为您制作饼图。