自定义圆形进度条

#import <UIKit/UIKit.h>

@interface CircleProgressView : UIView

/**进度值*/
@property(nonatomic,assign) CGFloat  progressValue;

@end
#import "CircleProgressView.h"

@implementation CircleProgressView
@synthesize progressValue = _progressValue;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    [self setNeedsDisplay];
    
}

- (CGFloat)progressValue
{
    return _progressValue;

}

- (void)drawRect:(CGRect)rect
{
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGFloat radius = rect.size.width/2;
    
    /**绘制一个灰色背景的圆形*/
    CGContextAddArc(contextRef, radius, radius, radius, 0, 3.141596253*2, 0);
    CGContextSetRGBFillColor(contextRef, 0.7, 0.7, 0.7, 1);
    CGContextFillPath(contextRef);
    
    /**绘制一个动态圆形*/
    CGContextAddArc(contextRef, radius, radius, radius, 0, 3.14 * _progressValue * 2, 0);
    CGContextAddLineToPoint(contextRef, radius, radius);
    CGContextSetRGBFillColor(contextRef, 0, 0, 1, 1);
    CGContextFillPath(contextRef);

}
@end
#import "ViewController.h"
#import "CircleProgressView.h"
@interface ViewController ()
{

   CircleProgressView *_vCircleProgress;
   NSTimer *_timer;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _vCircleProgress = [[CircleProgressView alloc]initWithFrame:CGRectMake(0,0, 100, 100)];
    _vCircleProgress.center = self.view.center;
    [self.view addSubview:_vCircleProgress];
    _timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changeProgressValue) userInfo:nil repeats:YES];

}
- (void)changeProgressValue
{
    CGFloat value = arc4random()%100/100.f;
    _vCircleProgress.progressValue = value;
}

@end

转载于:https://www.cnblogs.com/thbbsky/p/4386091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值