iOS帶動畫的環形進度條(進度條和數字同步)


本篇寫的是實現環形進度條,並帶動畫效果,要實現這些,僅能通過自己畫一個

方法直接看代碼  demo下載:LoopProgressViewDemo.zip

為了方便多次調用,用繼承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的調用並非精確,可以自行百度

 這里因為每0.01s啟動一次定時器,所以要同步進度條和數字,就將self.progress賦值給動畫的duration屬性就可以了,duration為動畫時間

 在使用時我發現如果在tableviewcell中添加了這個環形進度條時有個缺點,就是定時器原本用的是系統的runloop,導致數據顯示滯后,所以現更新為子線程里添加定時器,子線程的定時器必須添加

[[NSRunLoop currentRunLoop] run];才可啟動定時器,因為子線程的runloop里是不帶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 
 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-1

1     LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
2     custom.progress = 0.44;
3     [self.view addSubview:custom];

 

實現后:

已經實現進度條和數字的同步:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM