进度条简单封装

先上效果图,很简单不喜勿喷,有错的地方,还请指正


ZRProgress1.gif

半透明的圆按百分比消失

ZRUploadProgressView.h文件

#import <UIKit/UIKit.h>

typedef void(^ZRCompletionBlock)(void);

@interface ZRUploadProgressView : UIView

@property (nonatomic, strong) UIColor *progressColor;
// 0~1 小数
@property (nonatomic, assign) CGFloat progress;

@property (nonatomic, strong) NSString *imageName;

@property (nonatomic, copy) ZRCompletionBlock completionBlock;

@end

ZRUploadProgressView.m文件

#import "ZRUploadProgressView.h"

@interface ZRUploadProgressView ()

@property (nonatomic, strong) CAShapeLayer *trackLayer;
@property (nonatomic, strong) CAShapeLayer *imageLayer;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIBezierPath *progressPath;

@end

@implementation ZRUploadProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self drawArcProgressView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self drawArcProgressView];
    }
    return self;
}

- (void)drawArcProgressView {
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = width/2.0;
    
    self.imageLayer = [CAShapeLayer layer];
    self.imageLayer.frame = self.bounds;
    [self.layer addSublayer:self.imageLayer];
    
    CGPoint center = CGPointMake(width/2, height/2);
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:center radius:(width)/ 2 startAngle:- M_PI_2 endAngle: -M_PI*5/2 clockwise:NO];
    self.trackLayer = [CAShapeLayer layer];
    self.trackLayer.frame = CGRectMake(0, 0, width, height);
    self.trackLayer.fillColor = [UIColor clearColor].CGColor;
    self.trackLayer.strokeColor = [UIColor orangeColor].CGColor;
    self.trackLayer.lineWidth = width;
    self.trackLayer.path = progressPath.CGPath;
    self.trackLayer.strokeEnd = 1;
    [self.layer addSublayer:self.trackLayer];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
    self.titleLabel.center = center;
    self.titleLabel.hidden = YES;
    self.titleLabel.textColor = [UIColor yellowColor];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.titleLabel];
}

- (void)setProgressColor:(UIColor *)progressColor {
    self.trackLayer.strokeColor = progressColor.CGColor;
}

- (void)setTitleColor:(UIColor *)titleColor {
    self.titleLabel.textColor =  titleColor;
}

- (void)setImageName:(NSString *)imageName {
    [self.imageLayer setContents:(id)[UIImage imageNamed:imageName].CGImage];
}

- (void)setProgress:(CGFloat)progress {
    _progress = MIN(1, MAX(0, progress));
   _progress = 1.0 - progress;
    self.trackLayer.strokeEnd = _progress;
    NSString *progressStr = [NSString stringWithFormat:@"%.1f%%",100.0-_progress * 100];
    self.titleLabel.text = progressStr;
    if (_progress <= 0) {
        if (self.completionBlock) {
            self.completionBlock();
            self.completionBlock = nil;
        }
    } else {
        self.titleLabel.hidden = NO;
    }
}

圆环按百分比加载

ZRCircleProgressView.h

#import <UIKit/UIKit.h>

typedef void(^ZRCompletionBlock)(void);

@interface ZRCircleProgressView : UIView

@property (nonatomic, strong) UIColor *progressColor;
@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, assign) CGFloat progressWidth;
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, copy) ZRCompletionBlock completionBlock;

@end

ZRCircleProgressView.m

#import "ZRCircleProgressView.h"

@interface ZRCircleProgressView()

@property (nonatomic, strong) CAShapeLayer *circleLayer;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIBezierPath *path;

@end

@implementation ZRCircleProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = width/2.0;
    self.circleLayer = [CAShapeLayer layer];
    self.circleLayer.frame = self.bounds;
    self.circleLayer.strokeEnd = 0;
    self.circleLayer.lineWidth = 3;
    self.circleLayer.strokeColor = [UIColor orangeColor].CGColor;
    self.circleLayer.fillColor = [UIColor clearColor].CGColor;
    CGPoint center = CGPointMake(width/2, height/2);
    [self.layer addSublayer:self.circleLayer];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
    self.titleLabel.center = center;
    self.titleLabel.hidden = YES;
    self.titleLabel.textColor = [UIColor yellowColor];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.titleLabel];
}

- (void)setTitleColor:(UIColor *)titleColor {
    self.titleLabel.textColor =  titleColor;
}

- (void)setProgressColor:(UIColor *)progressColor {
    self.circleLayer.strokeColor = progressColor.CGColor;
}

- (void)setProgress:(CGFloat)progress {
    _progress = MIN(1, MAX(0, progress));
    self.circleLayer.strokeEnd = _progress;
    NSString *progressStr = [NSString stringWithFormat:@"%.1f%%",_progress * 100];
    self.titleLabel.text = progressStr;
    if (_progress >= 1) {
        if (self.completionBlock) {
            self.completionBlock();
            self.completionBlock = nil;
        }
    } else {
        self.titleLabel.hidden = NO;
    }
}

- (void)setProgressWidth:(CGFloat)progressWidth {
    if (!_path) {
        CGFloat width = self.frame.size.width;
        CGPoint center = CGPointMake(width/2, width/2);
        _path = [UIBezierPath bezierPathWithArcCenter:center radius:(width/2 - progressWidth/2) startAngle:-M_PI_2 endAngle:3*M_PI/2 clockwise:YES];
        self.circleLayer.path = _path.CGPath;
    }
    self.circleLayer.lineWidth = progressWidth;
}

使用

#import "ViewController.h"
#import "ZRUploadProgressView.h"
#import "ZRCircleProgressView.h"

@interface ViewController ()

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, strong) ZRUploadProgressView *uploadView;
@property (nonatomic, strong) ZRCircleProgressView *circleView;
@property (nonatomic, strong) ZRCircleProgressView *circleView1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.progress = 0.0f;
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat width = (screenWidth-80)/3;
    CGFloat orgY = [UIScreen mainScreen].bounds.size.height/2 - width/2;
    
    self.uploadView = [[ZRUploadProgressView alloc] initWithFrame:CGRectMake(20, orgY, width, width)];
    self.uploadView.progressColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.uploadView.imageName = @"1.jpeg";
    self.uploadView.backgroundColor = [UIColor clearColor];
    self.uploadView.completionBlock = ^{
        NSLog(@"uploadView - completion");
    };
    [self.view addSubview:self.uploadView];
    
    self.circleView = [[ZRCircleProgressView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.uploadView.frame) + 20, orgY, width, width)];
    self.circleView.backgroundColor = [UIColor cyanColor];
    self.circleView.progressWidth = width/2;
    self.circleView.completionBlock = ^{
        NSLog(@"circleView - completion");
    };
    [self.view addSubview:self.circleView];
    
    self.circleView1 = [[ZRCircleProgressView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.circleView.frame) + 20, orgY, width, width)];
    self.circleView1.backgroundColor = [UIColor blueColor];
    self.circleView1.progressWidth = 10;
    self.circleView1.completionBlock = ^{
        NSLog(@"circleView1 - completion");
    };
    [self.view addSubview:self.circleView1];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)makeTimer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

- (void)startTimer {
    
    CGFloat progressRandom =  (arc4random()%100)/10000.0;
    self.progress += progressRandom;
    if (self.progress >= 1) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.uploadView.progress = self.progress;
    self.circleView.progress = self.progress;
    self.circleView1.progress = self.progress;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 模拟下载动作
    [self makeTimer];
}

github传送门ZRProgress记得留下你的Star,Star是我更新的动力

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容