iOS带动画的环形进度条
本篇写的是实现环形进度条,并带动画效果,要实现这些,仅能通过自己画一个
方法直接看代码
为了方便多次调用,用继承UIView的方式
.m文件
1 #import <UIKit/UIKit.h> 2 3 @interface LoopProgressView : UIView 4 5 @property (nonatomic, assign) CGFloat progress; 6 7 8 @end
.h文件
NSTimer的调用并非精确,可以自行百度
1 #import "LoopProgressView.h" 2 #import <QuartzCore/QuartzCore.h> 3 4 #define ViewWidth self.frame.size.width //环形进度条的视图宽度 5 #define ProgressWidth 2.5 //环形进度条的圆环宽度 6 #define Radius ViewWidth/2-ProgressWidth //环形进度条的半径 7 8 @interface LoopProgressView() 9 { 10 CAShapeLayer *arcLayer; 11 UILabel *label; 12 NSTimer *progressTimer; 13 } 14 @property (nonatomic,assign)CGFloat i; 15 @end 16 17 @implementation LoopProgressView 18 19 -(id)initWithFrame:(CGRect)frame 20 { 21 self = [super initWithFrame:frame]; 22 if (self) { 23 self.backgroundColor = [UIColor clearColor]; 24 } 25 return self; 26 } 27 28 -(void)drawRect:(CGRect)rect 29 { 30 _i=0; 31 CGContextRef progressContext = UIGraphicsGetCurrentContext(); 32 CGContextSetLineWidth(progressContext, ProgressWidth); 33 CGContextSetRGBStrokeColor(progressContext, 209.0/255.0, 209.0/255.0, 209.0/255.0, 1); 34 35 CGFloat xCenter = rect.size.width * 0.5; 36 CGFloat yCenter = rect.size.height * 0.5; 37 38 //绘制环形进度条底框 39 CGContextAddArc(progressContext, xCenter, yCenter, Radius, 0, 2*M_PI, 0); 40 CGContextDrawPath(progressContext, kCGPathStroke); 41 42 // //绘制环形进度环 43 CGFloat to = self.progress * M_PI * 2; // - M_PI * 0.5为改变初始位置,此处可修改进度条的初始启动位置 44 45 // 进度数字字号,可自己根据自己需要,从视图大小去适配字体字号,这里就不做了 46 int fontNum = 15; 47 48 label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, Radius+10, 20)]; 49 label.center = CGPointMake(xCenter, yCenter); 50 label.textAlignment = NSTextAlignmentCenter; 51 label.font = [UIFont boldSystemFontOfSize:fontNum]; 52 label.text = @"0%"; 53 [self addSubview:label]; 54 55 UIBezierPath *path=[UIBezierPath bezierPath]; 56 [path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle:0 endAngle:to clockwise:YES]; 57 arcLayer=[CAShapeLayer layer]; 58 arcLayer.path=path.CGPath;//46,169,230 59 arcLayer.fillColor = [UIColor clearColor].CGColor; 60 arcLayer.strokeColor=[UIColor colorWithRed:227.0/255.0 green:91.0/255.0 blue:90.0/255.0 alpha:0.7].CGColor; 61 arcLayer.lineWidth=ProgressWidth; 62 arcLayer.backgroundColor = [UIColor blueColor].CGColor; 63 [self.layer addSublayer:arcLayer]; 64 65 66 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 67 [self drawLineAnimation:arcLayer]; 68 }); 69 70 if (self.progress>0) { 71 progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES]; 72 } 73 74 } 75 76 //NSTimer不会精准调用 77 -(void)timeLabel 78 { 79 _i += 0.01; 80 label.text = [NSString stringWithFormat:@"%.0f%%",_i*100]; 81 82 if (_i>=self.progress) { 83 [progressTimer invalidate]; 84 progressTimer = nil; 85 86 } 87 88 } 89 90 //定义动画过程 91 -(void)drawLineAnimation:(CALayer*)layer 92 { 93 CABasicAnimation *bas=[CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 94 bas.duration=1; 95 bas.delegate=self; 96 bas.fromValue=[NSNumber numberWithInteger:0]; 97 bas.toValue=[NSNumber numberWithInteger:1]; 98 [layer addAnimation:bas forKey:@"key"]; 99 } 100 101 @end
完成后在要调用的控制器里,仅需几段代码:传进的参数:为0-1
1 LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; 2 custom.progress = 0.44; 3 [self.view addSubview:custom];
实现后:
这里没有对数字和进度条做同步处理,有兴趣的可以尝试下: