首页 技术 正文
技术 2022年11月20日
0 收藏 818 点赞 4,624 浏览 4626 个字

.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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,702
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741