.h文件

#import <UIKit/UIKit.h>

@interface YTProgressView : UIView
@property (nonatomic, copy) NSString *progressString;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, copy) UIColor *progressColor;
- (void)updateViewsWithProgresSting:(NSString *)progressString progressColor:(UIColor *)progressColor progress:(CGFloat)progress;
/*
 带动画环形进度条
 */

@end

.m文件

#import "YTProgressView.h"
#import <QuartzCore/QuartzCore.h>

#define ViewWidth self.frame.size.width   //环形进度条的视图宽度
#define ProgressWidth 3                 //环形进度条的圆环宽度
#define Radius ViewWidth/2-ProgressWidth  //环形进度条的半径
#define RGBA(r, g, b, a)   [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:(a)]
#define RGB(r, g, b)        RGBA(r, g, b, 1.0)

@interface YTProgressView()<CAAnimationDelegate>
{
    CAShapeLayer *arcLayer;
    NSTimer *progressTimer;
}
@property (nonatomic,assign)CGFloat i;

@end

@implementation LoopProgressView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
YTLog(@"----progress %.f--%@",self.progress,self.progressString);
    _i=0;
    CGContextRef progressContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(progressContext, ProgressWidth);
    CGContextSetRGBStrokeColor(progressContext, 198.0/255.0, 198.0/255.0, 198.0/255.0, 1);

CGFloat xCenter = rect.size.width * 0.5;
    CGFloat yCenter = rect.size.height * 0.5;

//绘制环形进度条底框
    CGContextAddArc(progressContext, xCenter, yCenter, Radius, 0, 2*M_PI, 0);
    CGContextDrawPath(progressContext, kCGPathStroke);

//    //绘制环形进度环
        //    CGFloat to = - M_PI * 0.5 + self.progress * M_PI *2; // - M_PI * 0.5为改变初始位置

// 进度数字字号,可自己根据自己需要,从视图大小去适配字体字号
        //    int fontNum = ViewWidth/6;
    int weight = ViewWidth - ProgressWidth*2;
    if (!_label) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, weight, ViewWidth/6)];
        _label.center = CGPointMake(xCenter, yCenter);
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont boldSystemFontOfSize:14];
        _label.textColor = self.progressColor;
        _label.adjustsFontSizeToFitWidth = YES;
        [self addSubview:_label];
    }

_label.text = self.progressString ;
    YTLog(@"progress %.2f--%@",self.progress,self.progressString);
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle:- M_PI_2 endAngle:(-M_PI_2 +2*self.progress*M_PI) clockwise:YES];

if (!arcLayer) {
        arcLayer=[CAShapeLayer layer];
        arcLayer.path=path.CGPath;//46,169,230
        arcLayer.fillColor = [UIColor clearColor].CGColor;
        arcLayer.strokeColor=self.progressColor.CGColor;
        arcLayer.lineWidth=ProgressWidth;
        arcLayer.lineCap = @"round";
        arcLayer.backgroundColor = [UIColor blueColor].CGColor;

[self.layer addSublayer:arcLayer];
    }

[self progressAnimationStart];
}
- (void)updateViewsWithProgresSting:(NSString *)progressString progressColor:(UIColor *)progressColor progress:(CGFloat)progress{
    self.backgroundColor = [UIColor clearColor];
    self.progressColor = progressColor;
    self.progressString = progressString;
    self.progress = progress;
    self.label.text = progressString;
    self.label.textColor = progressColor;
    UIBezierPath *path=[UIBezierPath bezierPath];
    CGFloat xCenter = self.frame.size.width * 0.5;
    CGFloat yCenter = self.frame.size.height * 0.5;
    [path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle:- M_PI_2 endAngle:(-M_PI_2 +2*progress*M_PI) clockwise:YES];
    arcLayer.path=path.CGPath;
    arcLayer.strokeColor=progressColor.CGColor;
    [self progressAnimationStart];
}
- (void)progressAnimationStart
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self drawLineAnimation:arcLayer];
    });
    
    if (self.progress >= 1) {
        YTLog(@"传入数值范围为 0-1");
        self.progress = 1;
    }else if (self.progress <= 0){
        YTLog(@"传入数值范围为 0-1");
        self.progress = 0;
        return;
    }
    
    if (self.progress >= 0) {
        NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(newThread) object:nil];
        [thread start];
    }
}
-(void)newThread
{
    @autoreleasepool {
        progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] run];
    }
}

//NSTimer不会精准调用  虚拟机和真机效果不一样
-(void)timeLabel
{
    _i += 0.01;
    if (_i >= self.progress) {
        [progressTimer invalidate];
        progressTimer = nil;

}

}

//定义动画过程
-(void)drawLineAnimation:(CALayer*)layer
{
    CABasicAnimation *bas=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    bas.duration=self.progress;//动画时间
    bas.delegate=self;
    bas.fromValue=[NSNumber numberWithInteger:0];
    bas.toValue=[NSNumber numberWithInteger:1];
    [layer addAnimation:bas forKey:@"key"];
}

@end

iOS 环形进度条的更多相关文章

  1. iOS 开发技巧-制作环形进度条

    有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先看一下这篇博客,博客地址:ht ...

  2. iOS一分钟学会环形进度条

    有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现.先看一下这篇博客,博客地址:htt ...

  3. iOS带动画的环形进度条(进度条和数字同步)

    本篇写的是实现环形进度条,并带动画效果,要实现这些,仅能通过自己画一个 方法直接看代码 为了方便多次调用,用继承UIView的方式 .m文件 #import <UIKit/UIKit.h> ...

  4. [Swift通天遁地]一、超级工具-(2)制作美观大方的环形进度条

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. 图解CSS3制作圆环形进度条的实例教程

    圆环形进度条制作的基本思想还是画出基本的弧线图形,然后CSS3中我们可以控制其旋转来串联基本图形,制造出部分消失的效果,下面就来带大家学习图解CSS3制作圆环形进度条的实例教程 首先,当有人说你能不能 ...

  6. Swift - 环形进度条(UIActivityIndicatorView)的用法

    Swift中,除了条形进度条外,还有环形进度条,效果图如下: 1,环形进度条的基本属性 (1)Style: Large White:比较大的白色环形进度条 White:白色环形进度条 Gray:灰色环 ...

  7. 环形进度条的实现方法总结和动态时钟绘制(CSS3、SVG、Canvas)

    缘由: 在某一个游戏公司的笔试中,最后一道大题是,“用CSS3实现根据动态显示时间和环形进度[效果如下图所示],且每个圆环的颜色不一样,不需要考虑IE6~8的兼容性”.当时第一想法是用SVG,因为SV ...

  8. Android简易实战教程--第十七话《自定义彩色环形进度条》

    转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203533   点击打开链接 在Android初级教程里面,介绍了shape用法 ...

  9. canvas绘制环形进度条

    <!DOCTYPE html> <html > <head> <meta http-equiv="content-type" conten ...

随机推荐

  1. freeRTOS学习8-20

  2. English 邮件

    1.email http://www2.elc.polyu.edu.hk/cill/eiw/e-mail.htm

  3. Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  4. Docker 运行 MYSQL 数据库的总结

    公司里面要求做一个小demo 学习java相关的东西 然后使用了mysql数据库 很早之前做过mysql的容器化运行. 现在想想已经忘记的差不多了  所以这里总结一下 docker化运行mysql数据 ...

  5. SQLite基础-7.子句(一)

    目录 SQLite子句(一) 1. WHERE子句 2. LIKE子句 3. GLOB 子句 4. Oreder By 子句 SQLite子句(一) 1. WHERE子句 WHERE 子句后面跟着条件 ...

  6. 第k大异或值

    这道题与2018年十二省联考中的异或粽子很相像,可以算作一个简易版: 因为这不需要可持久化: 也就是说求任意两个数异或起来的第k大值: 首先把所有数放进trie里. 然后二分答案,枚举每个数,相应地在 ...

  7. PAT B1020 月饼(25)

    题目描述 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部 ...

  8. 简单使用template-web.js

    手册地址: http://aui.github.io/art-template/docs/syntax.html https://github.com/aui/art-template 原文地址: h ...

  9. 搞懂Dubbo服务发布与服务注册

    一.前言 本文讲服务发布与服务注册,服务提供者本地发布服务,然后向注册中心注册服务,将服务实现类以服务接口的形式提供出去,以便服务消费者从注册中心查阅并调用服务. 本文源码分析基于org.apache ...

  10. how to Simply Singleton Navigate the deceptively simple Singleton pattern---reference

    http://www.javaworld.com/article/2073352/core-java/simply-singleton.html JAVA DESIGN PATTERNS By Dav ...