本篇写的是实现环形进度条,并带动画效果,要实现这些,仅能通过自己画一个
为了方便多次调用,用继承UIView的方式
.m文件
1 #import
2
3 @interfaceLoopProgressView : UIView4
5 @property (nonatomic, assign) CGFloat progress;6
7
8 @end
.h文件
NSTimer的调用并非精确,可以自行百度
这里因为每0.01s启动一次定时器,所以要同步进度条和数字,就将self.progress赋值给动画的duration属性就可以了,duration为动画时间
在使用时我发现如果在tableviewcell中添加了这个环形进度条时有个缺点,就是定时器原本用的是系统的runloop,导致数据显示滞后,所以现更新为子线程里添加定时器,子线程的定时器必须添加
[[NSRunLoop currentRunLoop] run];才可启动定时器,因为子线程的runloop里是不带nstimer的,要手动添加运行
1 #import "LoopProgressView.h"
2 #import
3
4 #define ViewWidth self.frame.size.width //环形进度条的视图宽度
5 #define ProgressWidth 2.5 //环形进度条的圆环宽度
6 #define Radius ViewWidth/2-ProgressWidth //环形进度条的半径
7
8 @interfaceLoopProgressView()9 {10 CAShapeLayer *arcLayer;11 UILabel *label;12 NSTimer *progressTimer;13 }14 @property (nonatomic,assign)CGFloat i;15
16 @end
17
18 @implementationLoopProgressView19
20 -(id)initWithFrame:(CGRect)frame21 {22 self =[super initWithFrame:frame];23 if(self) {24 self.backgroundColor =[UIColor clearColor];25 }26 returnself;27 }28
29 -(void)drawRect:(CGRect)rect30 {31 _i=0;32 CGContextRef progressContext =UIGraphicsGetCurrentContext();33 CGContextSetLineWidth(progressContext, ProgressWidth);34 CGContextSetRGBStrokeColor(progressContext, 209.0/255.0, 209.0/255.0, 209.0/255.0, 1);35
36 CGFloat xCenter = rect.size.width * 0.5;37 CGFloat yCenter = rect.size.height * 0.5;38
39 //绘制环形进度条底框
40 CGContextAddArc(progressContext, xCenter, yCenter, Radius, 0, 2*M_PI, 0);41 CGContextDrawPath(progressContext, kCGPathStroke);42
43 // //绘制环形进度环
44 CGFloat to = self.progress * M_PI * 2; //- M_PI * 0.5为改变初始位置45
46 //进度数字字号,可自己根据自己需要,从视图大小去适配字体字号
47 int fontNum = ViewWidth/6;
4849 label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,Radius+10, ViewWidth/6)];
50 label.center =CGPointMake(xCenter, yCenter);
51 label.textAlignment =NSTextAlignmentCenter;52 label.font =[UIFont boldSystemFontOfSize:fontNum];53 label.text = @"0%";54 [self addSubview:label];55
56 UIBezierPath *path=[UIBezierPath bezierPath];57 [path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle:0endAngle:to clockwise:YES];58 arcLayer=[CAShapeLayer layer];59 arcLayer.path=path.CGPath;//46,169,230
60 arcLayer.fillColor =[UIColor clearColor].CGColor;61 arcLayer.strokeColor=[UIColor colorWithRed:227.0/255.0 green:91.0/255.0 blue:90.0/255.0 alpha:0.7].CGColor;62 arcLayer.lineWidth=ProgressWidth;63 arcLayer.backgroundColor =[UIColor blueColor].CGColor;64 [self.layer addSublayer:arcLayer];65
66
67 dispatch_async(dispatch_get_global_queue(0, 0), ^{68 [self drawLineAnimation:arcLayer];69 });70
71 if (self.progress > 1) {72 NSLog(@"传入数值范围为 0-1");73 self.progress = 1;74 }else if (self.progress < 0){75 NSLog(@"传入数值范围为 0-1");76 self.progress = 0;77 return;78 }79
80 if (self.progress > 0) {81 NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(newThread) object:nil];82 [thread start];83 }84
85 }86
87 -(void)newThread88 {89 @autoreleasepool {90 progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];91 [[NSRunLoop currentRunLoop] run];92 }93 }94
95 //NSTimer不会精准调用
96 -(void)timeLabel97 {98 _i += 0.01;99 label.text = [NSString stringWithFormat:@"%.0f%%",_i*100];100
101 if (_i >=self.progress) {
102 [progressTimer invalidate];
103 progressTimer =nil;104
105 }106
107 }108
109 //定义动画过程
110 -(void)drawLineAnimation:(CALayer*)layer111 {112 CABasicAnimation *bas=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];113 bas.duration=self.progress;//动画时间
114 bas.delegate=self;115 bas.fromValue=[NSNumber numberWithInteger:0];116 bas.toValue=[NSNumber numberWithInteger:1];117 [layer addAnimation:bas forKey:@"key"];118 }119
120 @end
完成后在要调用的控制器里,仅需几段代码:传进的参数:为0-1
1 LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];2 custom.progress = 0.44;3 [self.view addSubview:custom];
实现后:
已经实现进度条和数字的同步: