效果图
代码 // // XRProgressView.h // 使用CALayer封装下载进度条 // // Created by 寒竹子 on 15/6/14. // Copyright (c) 2015年 寒竹子. All rights reserved. // /** * 下载进度条封装 */ #import <UIKit/UIKit.h> @interface XRProgressView : UIView @property (nonatomic, assign) CGFloat progress; @property (nonatomic, strong) UIColor * progressColor; @end // // XRProgressView.m // 使用CALayer封装下载进度条 // // Created by 寒竹子 on 15/6/14. // Copyright (c) 2015年 寒竹子. All rights reserved. // #import "XRProgressView.h" @interface XRProgressView () @property (nonatomic, strong) CALayer * progressLayer; // 进度layer @property (nonatomic, assign) CGFloat currentWidth; // 当前layer的width @end @implementation XRProgressView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.layer.backgroundColor = [UIColor clearColor].CGColor; self.layer.borderWidth = .5f;
// 创建layer _progressLayer = [CALayer layer]; _progressLayer.frame = CGRectMake(0, 0, 0, self.frame.size.height);
// 将progressLayer添加到当前View的layer中 [self.layer addSublayer:_progressLayer];
// 保存width _currentWidth = self.frame.size.width; }
return self; } #pragma mark - getters and setters @synthesize progress = _progress; // 设置进度 - (void)setProgress:(CGFloat)progress { _progress = progress;
if (_progress < 0.0f) { self.progressLayer.frame = CGRectMake(0, 0, 0, self.frame.size.height); }else if (_progress <= 1.0f) { self.progressLayer.frame = CGRectMake(0, 0, _progress * self.currentWidth, self.frame.size.height); }else { self.progressLayer.frame = CGRectMake(0, 0, self.currentWidth, self.frame.size.height); } } - (CGFloat)progress { return _progress; } @synthesize progressColor = _progressColor; // 设置layer背景颜色 - (void)setProgressColor:(UIColor *)progressColor { _progressColor = progressColor; self.progressLayer.backgroundColor = _progressColor.CGColor; } - (UIColor *)progressColor { return _progressColor; } @end // // ViewController.m // 使用CALayer封装下载进度条 // // Created by 寒竹子 on 15/6/14. // Copyright (c) 2015年 寒竹子. All rights reserved. // #define KBorder 20.0f #import "ViewController.h" #import "XRProgressView.h" @interface ViewController () @property (nonatomic, strong) XRProgressView * progressView; @property (nonatomic, strong) NSTimer * timer; @property (nonatomic, assign) CGFloat progress; // 下载进度 @end @implementation ViewController - (instancetype)init { if (self = [super init]) { _progress = 0.0f; }
return self; } // 起定时器 - (void)startTimer { _timer = [NSTimer scheduledTimerWithTimeInterval:.1f target:self selector:@selector(updateProgress) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; } // 模拟更新下载进度 - (void)updateProgress { if (_progress > 1.0f) { _progress = 0.0f; }else { _progress += .01f; } self.progressView.progress = _progress; } - (void)viewDidLoad { [super viewDidLoad];
self.progressView = [[XRProgressView alloc] initWithFrame:CGRectMake(KBorder, KBorder * 2.0f, self.view.frame.size.width - KBorder * 2.0f, 4.0f)]; self.progressView.layer.masksToBounds = YES; self.progressView.layer.cornerRadius = self.progressView.frame.size.height / 2.0f; self.progressView.progressColor = [UIColor redColor]; [self.view addSubview:self.progressView];
// 开启定时器 [self startTimer]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end |