自定义圆形进度条

#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

posted @ 2015-04-02 10:13  曹县三胖暴打大猩猩  阅读(154)  评论(0编辑  收藏  举报