本篇寫的是實現環形進度條,並帶動畫效果,要實現這些,僅能經過本身畫一個async
方法直接看代碼 demo下載:LoopProgressViewDemo.zipoop
爲了方便屢次調用,用繼承UIView的方式字體
.m文件動畫
1 #import <UIKit/UIKit.h> 2 3 @interface LoopProgressView : UIView 4 5 @property (nonatomic, assign) CGFloat progress; 6 7 8 @end
.h文件atom
NSTimer的調用並不是精確,能夠自行百度spa
這裏由於每0.01s啓動一次定時器,因此要同步進度條和數字,就將self.progress賦值給動畫的duration屬性就能夠了,duration爲動畫時間線程
在使用時我發現若是在tableviewcell中添加了這個環形進度條時有個缺點,就是定時器本來用的是系統的runloop,致使數據顯示滯後,因此現更新爲子線程裏添加定時器,子線程的定時器必須添加code
[[NSRunLoop currentRunLoop] run];纔可啓動定時器,由於子線程的runloop裏是不帶nstimer的,要手動添加運行orm
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 16 @end 17 18 @implementation LoopProgressView 19 20 -(id)initWithFrame:(CGRect)frame 21 { 22 self = [super initWithFrame:frame]; 23 if (self) { 24 self.backgroundColor = [UIColor clearColor]; 25 } 26 return self; 27 } 28 29 -(void)drawRect:(CGRect)rect 30 { 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; 48
49 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:0 endAngle: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)newThread 88 { 89 @autoreleasepool { 90 progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES]; 91 [[NSRunLoop currentRunLoop] run]; 92 } 93 } 94 95 //NSTimer不會精準調用 96 -(void)timeLabel 97 { 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*)layer 111 { 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-1blog
1 LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; 2 custom.progress = 0.44; 3 [self.view addSubview:custom];
實現後:
已經實現進度條和數字的同步: