分段分颜色环形进度条

    xiaoxiao2021-03-25  87

    分段划曲线

    思路

    将第一段的终值赋值给第二段的初始值,以此类推。。。 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方法 }

    转载请注明原文地址: https://ju.6miu.com/read-22190.html

    最新回复(0)