分段分颜色环形进度条

来源:互联网 发布:百度 程序员 签字费 编辑:程序博客网 时间:2024/06/07 09:03

分段划曲线

思路

将第一段的终值赋值给第二段的初始值,以此类推。。。2.[self setNeedsDisplay]更新画图进度

代码示例

/* 显示多段不同比例的进度 */#import <UIKit/UIKit.h>#import "HXProgressColorAndRateModel.h"@interface PieProgressView : UIView@property(nonatomic,strong) NSMutableArray *colors;//显示对应的进度和颜色,模型数组@property(nonatomic,strong) UIColor *defaultColor;//只有一种颜色的时候的显示-(void)setProgressWithAnimated;@end#import "PieProgressView.h"#import <QuartzCore/QuartzCore.h>@interface PieProgressView()@property(nonatomic,assign) float start;@property(nonatomic,assign) float end;@end@implementation PieProgressView-(void)drawRect:(CGRect)rect{    /*     不断将end赋值给start,划线     */    CGContextRef context = UIGraphicsGetCurrentContext();    self.start = -M_PI_2;    CGPoint point = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);//中心位置    if (self.colors&&self.colors.count) {        for (int i=0; i<self.colors.count; i++) {            HXProgressColorAndRateModel *model = self.colors[i];            self.end=self.start + model.progressRate*2*M_PI;            CGFloat endAngle = self.end;            float radiusCircle = self.frame.size.width>self.frame.size.height?self.frame.size.height/2:self.frame.size.width/2;            UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:point radius:(radiusCircle-2) startAngle:self.start endAngle:endAngle clockwise:YES];            CGContextSetLineCap(context, kCGLineCapSquare);            CGContextSetLineWidth(context, 4.0);            CGContextSetStrokeColorWithColor(context, model.progressColor.CGColor);//部分颜色            CGContextAddPath(context, bezierPath.CGPath);            CGContextStrokePath(context);//渲染            self.start = self.end;        }    }else{//只有一种颜色时候        CGFloat endAngle = 2*M_PI;        float radiusCircle = self.frame.size.width>self.frame.size.height?self.frame.size.height/2:self.frame.size.width/2;        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:point radius:(radiusCircle-2) startAngle:self.start endAngle:endAngle clockwise:YES];        CGContextSetLineCap(context, kCGLineCapSquare);        CGContextSetLineWidth(context, 4.0);        //防止不赋值时候为黑色。。。        UIColor *deColor = self.defaultColor?self.defaultColor:[UIColor whiteColor];        CGContextSetStrokeColorWithColor(context, deColor.CGColor);//第一部分颜色        CGContextAddPath(context, bezierPath.CGPath);        CGContextStrokePath(context);//渲染    }}-(void)setProgressWithAnimated{    self.hidden = NO;    [self setNeedsDisplay];//调用drawRect方法}

这里写图片描述

0 0