#import <UIKit/UIKit.h> @interface LoopProgressView : UIView @property (nonatomic, assign) CGFloat progress; @end
#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
LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; custom.progress = 0.44; [self.view addSubview:custom];
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有