iOS 环形进度条
.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 环形进度条的更多相关文章
- iOS 开发技巧-制作环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先看一下这篇博客,博客地址:ht ...
- iOS一分钟学会环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现.先看一下这篇博客,博客地址:htt ...
- iOS带动画的环形进度条(进度条和数字同步)
本篇写的是实现环形进度条,并带动画效果,要实现这些,仅能通过自己画一个 方法直接看代码 为了方便多次调用,用继承UIView的方式 .m文件 #import <UIKit/UIKit.h> ...
- [Swift通天遁地]一、超级工具-(2)制作美观大方的环形进度条
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 图解CSS3制作圆环形进度条的实例教程
圆环形进度条制作的基本思想还是画出基本的弧线图形,然后CSS3中我们可以控制其旋转来串联基本图形,制造出部分消失的效果,下面就来带大家学习图解CSS3制作圆环形进度条的实例教程 首先,当有人说你能不能 ...
- Swift - 环形进度条(UIActivityIndicatorView)的用法
Swift中,除了条形进度条外,还有环形进度条,效果图如下: 1,环形进度条的基本属性 (1)Style: Large White:比较大的白色环形进度条 White:白色环形进度条 Gray:灰色环 ...
- 环形进度条的实现方法总结和动态时钟绘制(CSS3、SVG、Canvas)
缘由: 在某一个游戏公司的笔试中,最后一道大题是,“用CSS3实现根据动态显示时间和环形进度[效果如下图所示],且每个圆环的颜色不一样,不需要考虑IE6~8的兼容性”.当时第一想法是用SVG,因为SV ...
- Android简易实战教程--第十七话《自定义彩色环形进度条》
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203533 点击打开链接 在Android初级教程里面,介绍了shape用法 ...
- canvas绘制环形进度条
<!DOCTYPE html> <html > <head> <meta http-equiv="content-type" conten ...
随机推荐
- 【并行计算-CUDA开发】GPGPU OpenCL/CUDA 高性能编程的10大注意事项
GPGPU OpenCL/CUDA 高性能编程的10大注意事项 1.展开循环 如果提前知道了循环的次数,可以进行循环展开,这样省去了循环条件的比较次数.但是同时也不能使得kernel代码太大. 循环展 ...
- Linux、Aix(unix)、Oracle 银行外包开发运维常用命令
我一直是银行外包开发人员,常用的操作命令固然少不了,这是我一次自己边添加边使用的笔记.内容有点乱,希望可以帮到你. rm 文件或目录rm -f 文件或目录rm -rf * 跑路的时候用du -h 文件 ...
- python 下安装pymysql应用
前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11. ...
- Centos删除多余的Linux内核
删除开机多余kernel(centos) yum update 命令会大范围升级,有时候linux内核也会升级,升级后开机画面就会出现多个内核供你选择,所有强迫症的我需要删除旧的 ⒈查看正在使用的内核 ...
- Android Application的目录结构
目录结构: 1,java目录:保存java或kotlin源文件 2,res目录:保存Android项目的各种资源文件.比如layout子目录存放界面布局文件,values子目录存放各种XML格式的资源 ...
- Java实现二叉树地遍历、求深度和叶子结点的个数
一.分析 二叉树是n个结点所构成的集合,它或为空树,或为非空树.对于非空树,它有且仅有一个根结点,且除根结点以外的其余结点分为两个互不相交的子集,分别称为左子树和右子树,它们本身又都是二叉树. 显而易 ...
- [AHOI2017初中组]guide 题解
题面 我们无论怎么走,都是要从此点沿最短路径走到终点,所以我们以n为原点跑两边dijkstra就可以了: 而抱怨数可以根据之前跑出来的东西新建一个图,然后跑最短路就好了: #include <b ...
- centos7下NFS配置
NFS是Network File System的缩写,即网络文件系统.客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下. 前言 四台机器: ,218三台机器的/root/filedi ...
- redis 学习(19)-- RDB与AOF的抉择
RDB与AOF的抉择 1.RDB VS AOF RDB AOF 启动优先级 低 高 体积 小 大 恢复速度 快 慢 数据安全性 容易丢数据 根据策略决定 轻重 重 轻 2.RDB的最佳策略 关闭RDB ...
- Spingboot+Mybatis+Oracle项目配置
配置过程参考: 项目创建:http://how2j.cn/k/springboot/springboot-eclipse/1640.html 集成Mybatis使用Oracle:https://www ...