iOS实现带动画的环形进度条

所属分类: 软件编程 / IOS 阅读数: 1113
收藏 0 赞 0 分享

本篇写的是实现环形进度条,并带动画效果,要实现这些,仅能通过自己画一个

方法直接看代码

为了方便多次调用,用继承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=0;
 CGContextRef progressContext = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(progressContext, ProgressWidth);
 CGContextSetRGBStrokeColor(progressContext, 209.0/255.0, 209.0/255.0, 209.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 = self.progress * M_PI * 2; // - M_PI * 0.5为改变初始位置
 
 // 进度数字字号,可自己根据自己需要,从视图大小去适配字体字号
 int fontNum = ViewWidth/6;
 49 label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,Radius+10, ViewWidth/6)];
 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:0 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(0, 0), ^{
 [self drawLineAnimation:arcLayer];
 });
 
 if (self.progress > 1) {
 NSLog(@"传入数值范围为 0-1");
 self.progress = 1;
 }else if (self.progress < 0){
 NSLog(@"传入数值范围为 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;
 label.text = [NSString stringWithFormat:@"%.0f%%",_i*100];
 
 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
 

完成后在要调用的控制器里,仅需几段代码:传进的参数:为0-1

 LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
 custom.progress = 0.44;
 [self.view addSubview:custom]; 

实现:

已经实现进度条和数字的同步:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

详细整理iOS中UITableView的性能优化

最近在微博上看到一个很好的开源项目,是关于如何优化UITableView的,加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优化技巧,所以决定详细的整理下对优化UITableView的理解,需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

IOS开发中禁止NavigationController的向右滑动返回

这篇文章主要介绍了IOS开发中禁止NavigationController的向右滑动返回的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现微信/QQ显示最近拍摄图片的功能实例代码

如果你刚刚拍摄了图片,在使用微信/QQ发生消息时会显示“你可能要发送的图片”,这个功能非常人性化,怎么实现的呢?下面小编给大家分享iOS实现微信/QQ显示最近拍摄图片的功能实例代码,一起看看吧
收藏 0 赞 0 分享

iOS实现动态自适应标签

这篇文章主要为大家详细介绍了iOS动态自适应标签的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现图片存在本地、再从本地获取图片的功能

本文主要介绍了iOS实现图片存在本地、再从本地获取图片的功能的代码。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS视频录制(或选择)压缩及上传功能(整理)

最新做的一个功能涉及到了视频的录制、压缩及上传功能,经过大神的一番教导,终于倒腾清楚了,今天小编把问题经过记录一下分享到脚本之家平台,供大家参考
收藏 0 赞 0 分享

iOS中打包上传常见的错误与解决办法

关于打包上传至AppStore,大家都认为是最后一步了,其实到了这里往往会遇到很多的坑。对于踩过的坑我不想再踩第二遍,所以在此将我遇到的所有奇葩问题在此做一个记录,当作对自己的一个提醒,同时也分享给给位,需要的朋友可以参考下。
收藏 0 赞 0 分享

解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法

这篇文章主要介绍了关于解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法,需要的朋友可以参考下
收藏 0 赞 0 分享

Objective-C实现身份证验证的方法示例

这篇文章主要给大家分享了Objective-C实现身份证验证的方法,文中给出了详细的示例代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

ios启动页强制竖屏(进入App后允许横屏与竖屏)

最近工作遇到这样一个需要,当进入启动页需要强制竖屏,而进入APP后就允许横屏与竖屏,通过查找相关的资料找到了解决的方法,所以将实现的方法整理后分享出来,需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多