.h文件
#import <UIKit/UIKit.h>
@interface YTProgressView : UIView
@property (nonatomic, copy) NSString *progressString;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, copy) UIColor *progressColor;
- (void)updateViewsWithProgresSting:(NSString *)progressString progressColor:(UIColor *)progressColor progress:(CGFloat)progress;
@end
.m文件
#import "YTProgressView.h"
#import <QuartzCore/QuartzCore.h>
#define ViewWidth self.frame.size.width //环形进度条的视图宽度
#define ProgressWidth 3 //环形进度条的圆环宽度
#define Radius ViewWidth/2-ProgressWidth //环形进度条的半径
#define RGBA(r, g, b, a) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:(a)]
#define RGB(r, g, b) RGBA(r, g, b, 1.0)
@interface YTProgressView(){
CAShapeLayer *arcLayer;
NSTimer *progressTimer;
}
@property (nonatomic,assign)CGFloat i;
@end
@implementation LoopProgressView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
YTLog(@"----progress %.f--%@",self.progress,self.progressString);
_i=0;
CGContextRef progressContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(progressContext, ProgressWidth);
CGContextSetRGBStrokeColor(progressContext, 198.0/255.0, 198.0/255.0, 198.0/255.0, 1);
CGFloat xCenter = rect.size.width * 0.5;
CGFloat yCenter = rect.size.height * 0.5;
//绘制环形进度条底框
CGContextAddArc(progressContext, xCenter, yCenter, Radius, 0, 2*M_PI, 0);
CGContextDrawPath(progressContext, kCGPathStroke);
// //绘制环形进度环
// CGFloat to = - M_PI * 0.5 + self.progress * M_PI *2; // - M_PI * 0.5为改变初始位置
// 进度数字字号,可自己根据自己需要,从视图大小去适配字体字号
// int fontNum = ViewWidth/6;
int weight = ViewWidth - ProgressWidth*2;
if (!_label) {
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, weight, ViewWidth/6)];
_label.center = CGPointMake(xCenter, yCenter);
_label.textAlignment = NSTextAlignmentCenter;
_label.font = [UIFont boldSystemFontOfSize:14];
_label.textColor = self.progressColor;
_label.adjustsFontSizeToFitWidth = YES;
[self addSubview:_label];
}
_label.text = self.progressString ;
YTLog(@"progress %.2f--%@",self.progress,self.progressString);
UIBezierPath *path=[UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle:- M_PI_2 endAngle:(-M_PI_2 +2*self.progress*M_PI) clockwise:YES];
if (!arcLayer) {
arcLayer=[CAShapeLayer layer];
arcLayer.path=path.CGPath;//46,169,230
arcLayer.fillColor = [UIColor clearColor].CGColor;
arcLayer.strokeColor=self.progressColor.CGColor;
arcLayer.lineWidth=ProgressWidth;
arcLayer.lineCap = @"round";
arcLayer.backgroundColor = [UIColor blueColor].CGColor;
[self.layer addSublayer:arcLayer];
}
[self progressAnimationStart];
}
- (void)updateViewsWithProgresSting:(NSString *)progressString progressColor:(UIColor *)progressColor progress:(CGFloat)progress{
self.backgroundColor = [UIColor clearColor];
self.progressColor = progressColor;
self.progressString = progressString;
self.progress = progress;
self.label.text = progressString;
self.label.textColor = progressColor;
UIBezierPath *path=[UIBezierPath bezierPath];
CGFloat xCenter = self.frame.size.width * 0.5;
CGFloat yCenter = self.frame.size.height * 0.5;
[path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle:- M_PI_2 endAngle:(-M_PI_2 +2*progress*M_PI) clockwise:YES];
arcLayer.path=path.CGPath;
arcLayer.strokeColor=progressColor.CGColor;
[self progressAnimationStart];
}
- (void)progressAnimationStart
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self drawLineAnimation:arcLayer];
});
if (self.progress >= 1) {
YTLog(@"传入数值范围为 0-1");
self.progress = 1;
}else if (self.progress <= 0){
YTLog(@"传入数值范围为 0-1");
self.progress = 0;
return;
}
if (self.progress >= 0) {
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(newThread) object:nil];
[thread start];
}
}
-(void)newThread
{
@autoreleasepool {
progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] run];
}
}
//NSTimer不会精准调用 虚拟机和真机效果不一样
-(void)timeLabel
{
_i += 0.01;
if (_i >= self.progress) {
[progressTimer invalidate];
progressTimer = nil;
}
}
//定义动画过程
-(void)drawLineAnimation:(CALayer*)layer
{
CABasicAnimation *bas=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
bas.duration=self.progress;//动画时间
bas.delegate=self;
bas.fromValue=[NSNumber numberWithInteger:0];
bas.toValue=[NSNumber numberWithInteger:1];
[layer addAnimation:bas forKey:@"key"];
}
@end