iOS带动画的环形进度条(进度条和数字同步)
本篇写的是实现环形进度条,并带动画效果,要实现这些,仅能通过自己画一个
方法直接看代码 demo下载:LoopProgressViewDemo.zip
为了方便多次调用,用继承UIView的方式
.m文件
#import <UIKit/UIKit.h> @interface LoopProgressView : UIView @property (nonatomic, assign) CGFloat progress; @end
.h文件
NSTimer的调用并非精确,可以自行百度
这里因为每0.01s启动一次定时器,所以要同步进度条和数字,就将self.progress赋值给动画的duration属性就可以了,duration为动画时间
在使用时我发现如果在tableviewcell中添加了这个环形进度条时有个缺点,就是定时器原本用的是系统的runloop,导致数据显示滞后,所以现更新为子线程里添加定时器,子线程的定时器必须添加
[[NSRunLoop currentRunLoop] run];才可启动定时器,因为子线程的runloop里是不带nstimer的,要手动添加运行
#import "LoopProgressView.h"
#import <QuartzCore/QuartzCore.h> #define ViewWidth self.frame.size.width //环形进度条的视图宽度
#define ProgressWidth 2.5 //环形进度条的圆环宽度
#define Radius ViewWidth/2-ProgressWidth //环形进度条的半径 @interface LoopProgressView()
{
CAShapeLayer *arcLayer;
UILabel *label;
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
{
_i=;
CGContextRef progressContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(progressContext, ProgressWidth);
CGContextSetRGBStrokeColor(progressContext, 209.0/255.0, 209.0/255.0, 209.0/255.0, ); CGFloat xCenter = rect.size.width * 0.5;
CGFloat yCenter = rect.size.height * 0.5; //绘制环形进度条底框
CGContextAddArc(progressContext, xCenter, yCenter, Radius, , *M_PI, );
CGContextDrawPath(progressContext, kCGPathStroke); // //绘制环形进度环
CGFloat to = self.progress * M_PI * ; // - M_PI * 0.5为改变初始位置 // 进度数字字号,可自己根据自己需要,从视图大小去适配字体字号
int fontNum = ViewWidth/;
48
49 label = [[UILabel alloc]initWithFrame:CGRectMake(, ,Radius+10, ViewWidth/)];
label.center = CGPointMake(xCenter, yCenter);
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:fontNum];
label.text = @"0%";
[self addSubview:label]; UIBezierPath *path=[UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle: endAngle:to clockwise:YES];
arcLayer=[CAShapeLayer layer];
arcLayer.path=path.CGPath;//46,169,230
arcLayer.fillColor = [UIColor clearColor].CGColor;
arcLayer.strokeColor=[UIColor colorWithRed:227.0/255.0 green:91.0/255.0 blue:90.0/255.0 alpha:0.7].CGColor;
arcLayer.lineWidth=ProgressWidth;
arcLayer.backgroundColor = [UIColor blueColor].CGColor;
[self.layer addSublayer:arcLayer]; dispatch_async(dispatch_get_global_queue(, ), ^{
[self drawLineAnimation:arcLayer];
}); if (self.progress > ) {
NSLog(@"传入数值范围为 0-1");
self.progress = ;
}else if (self.progress < ){
NSLog(@"传入数值范围为 0-1");
self.progress = ;
return;
} if (self.progress > ) {
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;
label.text = [NSString stringWithFormat:@"%.0f%%",_i*]; 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:];
bas.toValue=[NSNumber numberWithInteger:];
[layer addAnimation:bas forKey:@"key"];
}
@end
完成后在要调用的控制器里,仅需几段代码:传进的参数:为0-1
LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(, , , )];
custom.progress = 0.44;
[self.view addSubview:custom];
实现后:
已经实现进度条和数字的同步:

iOS带动画的环形进度条(进度条和数字同步)的更多相关文章
- ios - 带动画圆形旋转的进度条
#import <UIKit/UIKit.h> @interface TJCircleProgressView : UIView /** * 图标 */ @property(nonatom ...
- 【iOS实现一个颜色渐变的弧形进度条】
在Github上看到一些进度条的功能,都是通过Core Graph来实现.无所谓正确与否,但是开发效率明显就差很多了,而且运行效率还是值得考究的.其实使用苹果提供的Core Animation能够非常 ...
- 一款基于jquery带百分比的响应式进度加载条
今天要给大家带来一款基于jquery带百分比的响应式进度加载条.这款加载条非常漂亮,而且带有进度的百度比,且在不同的百分比用的是不同的颜色.而且这款加载条采用了响应式设计,在不同的分辨率的显示器下完美 ...
- iOS补位动画、沙漏效果、移动UITableViewCell、模拟贪吃蛇、拖拽进度等源码
iOS精选源码 JHAlertView - 一款黑白配色的HUD之沙漏效果 继承UIButton的自定义按钮SPButton 用递归算法实现iOS补位动画 iOS 长按移动UITableViewCel ...
- Xamarin XAML语言教程使用方法设置进度条进度
Xamarin XAML语言教程使用方法设置进度条进度 在ProgressBar中定义了一个ProgressTo方法,此方法也可以用来对进度条当前的进行进行设置,ProgressTo与Progress ...
- Xamarin XAML语言教程使用Progress属性数据绑定设置进度条进度
Xamarin XAML语言教程使用Progress属性数据绑定设置进度条进度 开发者除了可以为ProgressBar定义的Progress属性直接赋双精度类型的值外,还可以通过数据绑定的方式为该属性 ...
- Xamarin XAML语言教程Progress属性设置进度条进度
Xamarin XAML语言教程Progress属性设置进度条进度 在图12.19~12.21中我们看到的是没有实现加载的进度条,即进度条的当前进度为0,如果开发者想要修改当前进度,可以使用两种方式: ...
- IOS下载查看PDF文件(有下载进度)
IOS(object-c) 下载查看 PDF 其实还是蛮容易操作的.在下载前,首先要把 IOS 可以保存文件的目录给过一遍: IOS 文件保存目录 IOS 可以自定义写入的文件目录,是很有限的,只能是 ...
- Android View 之进度条+拖动条+星级评论条....
PS:将来的你会感谢现在奋斗的自己.... 学习内容: 1.进度条 2.拖动条 3.星级评论条 1.进度条... 进图条这东西想必大家是很熟悉的...为了使用户不会觉得应用程序死掉了,因此 ...
随机推荐
- final .....finally ...... 和Finalize ......区别
一.性质不同 ()final为关键字: ()finalize()为方法: ()finally为为区块标志,用于try语句中: 二.作用 ()final为用于标识常量的关键字,final标识的关键字存储 ...
- 关于NPOI
1,使用using(声明对象);using让局部对象失效,使用它时,要么其包含的类实现IDispose接口,要么他的父类实现IDispose接口. 2,模糊查询:%代表0到多个任意字符:_代表一个任意 ...
- datatable删除行
先列出正确的写法,如果你只想马上改错就先复制吧, protected void deleteDataRow(int RowID,DataTable dt) { ; i >= ; i--) { i ...
- 导入项目时Loading descriptor ...
最近导入了一个项目,始终在Loading descriptor ...,很长时间都没有结束. 这是Eclipse在从java.sun.com的服务器上下载配置文件,下载速度过慢导致的,其实配置文件不是 ...
- Ajax 文件上传
原文地址:http://blog.sina.com.cn/s/blog_5d64f7e3010127ns.html 用到两个对象 第一个对象:FormData 第二个对象:XMLHttpRequest ...
- 2016暑假多校联合---To My Girlfriend
2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...
- 低信噪比的HTML5优化
百度搜索引擎建议是我们的HTML文件最好不要超过128KB,其实现在对于那些大文件搜索引擎也是很容易就抓取到的,只不过我们是尽量在可能的情况下把我们的网页代码越精简越好,我们要知道搜索引擎抓取网页的时 ...
- Scalaz(22)- 泛函编程思维: Coerce Monadic Thinking
马上进入新的一年2016了,来点轻松点的内容吧.前面写过一篇关于用Reader实现依赖注入管理的博文(Scalaz(16)- Monad:依赖注入-Dependency Injection By Re ...
- Verilog学习笔记基本语法篇(十三)...............Gate门
Verilog中已有一些建立好的逻辑门和开关的模型.在所涉及的模块中,可通过实例引用这些门与开关模型,从而对模块进行结构化的描述. 逻辑门: and (output,input,...) nand ( ...
- Guava学习笔记:Guava新增集合类型-Bimap
BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构. 通常情况下,我们在使用Java的Map时,往往是通过key来查找value的,但是如果出现下面一种场景的情况,我们就 ...