.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. 2019Java常见面试上

    一.开场白简单的介绍一下自己的工作经历与职责,在校或者工作中主要的工作内容,主要负责的内容:(你的信息一清二白的写在简历上,能答出来的最好写在上面,模棱两可不是很清楚的最好不要写,否则会被问的很尴尬) ...

  2. 【miscellaneous】多播的实现和需要注意的问题

    多播的实现和需要注意的问题          前段时间研究了一小段时间的网络多播问题,自己很有感触,把自己的经历写出来,希望有需要的可以少走一些弯路.          先说一下原理,我觉得这个还是需 ...

  3. 如何在picture上显示透明Label

  4. [转帖]RedHat 如何更改网卡名 从ens192 改为eth0的问题

    RedHat 如何更改网卡名 从ens192 改为eth0的问题 2017年03月27日 17:50:47 the_conquer_zzy 阅读数 2416   版权声明:本文为博主原创文章,遵循CC ...

  5. 在Docker Container 内部安装 Mono 的方法 ---From官网

    1.首先 mono 是什么 Mono是一个由Xamarin公司(先前是Novell,最早为Ximian)所主持的自由开放源代码项目. 该项目的目标是创建一系列匹配ECMA标准(Ecma-334和Ecm ...

  6. linux系统redis安装及使用

    1.下载redishttp://download.redis.io/releases/redis-5.0.5.tar.gz$ wget http://download.redis.io/release ...

  7. 菜单ACTION控制栏位字段编辑,点击菜单ACTION才能编辑指定的栏位

    范例(axmt500): 目的,控制新增的栏位(价格清单2),需点击菜单栏“修改价格清单2”才能对相应的栏位进行编辑修改,并记录修改人.日期: 1)在规格上增加新ACTION——action_modi ...

  8. 杭电2019多校第一场,Problem I,String 2019

    题目描述 Tom has a string containing only lowercase letters. He wants to choose a subsequence of the str ...

  9. 安装calico

    安装docker:https://www.cnblogs.com/cjsblogs/p/8717304.html 安装etcd集群:https://www.cnblogs.com/cjsblogs/p ...

  10. 4-Perl 数据类型

    1.Perl 数据类型Perl 是一种弱类型语言,所以变量不需要指定类型,Perl 解释器会根据上下文自动选择匹配类型.Perl 有三个基本的数据类型:标量.数组.哈希.以下是这三种数据类型的说明:1 ...