Draw part of a circle

后端 未结 8 927
北海茫月
北海茫月 2020-11-27 04:03

For an iPhone application I want to draw a circle, that is only for an x percentage filled.

Something like this:

8条回答
  •  眼角桃花
    2020-11-27 04:19

    CircleViewController.h

    #import 
    
    @interface CircleViewController : UIViewController
    
    @end
    

    CircleViewController.m

    #import "CircleViewController.h"
    #import "GraphView.h"
    
    @interface CircleViewController ()
    
    @end
    
    @implementation CircleViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        GraphView *graphView = [[GraphView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
        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
    

    GraphView.h

    #import 
    
    @interface GraphView : UIView
    
    @end
    

    GraphView.m

    #import "GraphView.h"
    
    @implementation GraphView
    
    - (void)drawRect:(CGRect)rect {
    
        CGPoint circleCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    
        [self drawCircleWithCircleCenter:(CGPoint) circleCenter radius:80 firstColor:[UIColor blueColor].CGColor secondeColor:[UIColor redColor].CGColor lineWidth:2 startDegree:0 currentDegree:90];
        //[self drawCircleWithCircleCenter2:(CGPoint) circleCenter radius:80 firstColor:[UIColor blueColor].CGColor secondeColor:[UIColor redColor].CGColor lineWidth:2 startDegree:0 currentDegree:90];
    }
    
    - (void)drawCircleWithCircleCenter:(CGPoint) circleCenter
                                radius:(CGFloat)radius
                                firstColor:(CGColorRef)firstColor
                                secondeColor:(CGColorRef)secondeColor
                                lineWidth:(CGFloat)lineWidth
                                startDegree:(float)startDegree
                                currentDegree:(float)endDegree {
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, lineWidth);
    
        CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);
    
        CGContextAddArc(context, circleCenter.x , circleCenter.y, radius, [self radians:startDegree], [self radians:endDegree], 0);
        CGContextSetFillColorWithColor(context, firstColor);
        CGContextFillPath(context);
    
        CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);
    
        CGContextAddArc(context, circleCenter.x, circleCenter.y, radius, [self radians:endDegree], [self radians:startDegree], 0);
        CGContextSetFillColorWithColor(context, secondeColor);
        CGContextFillPath(context);
    }
    
    - (void)drawCircleWithCircleCenter2:(CGPoint) circleCenter
                                radius:(CGFloat)radius
                            firstColor:(CGColorRef)firstColor
                          secondeColor:(CGColorRef)secondeColor
                             lineWidth:(CGFloat)lineWidth
                           startDegree:(float)startDegree
                         currentDegree:(float)endDegree {
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, lineWidth);
    
        CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);
    
        CGContextAddArc(context, circleCenter.x , circleCenter.y, radius, [self radians:startDegree], [self radians:endDegree], 0);
        CGContextSetFillColorWithColor(context, firstColor);
        CGContextFillPath(context);
    
        CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);
    
        CGContextAddArc(context, circleCenter.x, circleCenter.y, radius, [self radians:endDegree], [self radians:startDegree], 0);
        CGContextSetStrokeColorWithColor(context, secondeColor);
        CGContextStrokePath(context);
    }
    
    -(float) radians:(double) degrees {
        return degrees * M_PI / 180;
    }
    
    
    @end
    

    note: you can use one of the 2 methods: "drawCircleWithCircleCenter" or "drawCircleWithCircleCenter2"

    this code if you want to split cell on 2 parts only

    if you want to split cell on more than 2 parts you can check this : "Drawing a circle ,filled different parts with different color" and check the answer start with this Phrase "we have 6 class"

提交回复
热议问题