码迷,mamicode.com
首页 > 其他好文 > 详细

自定义圆形进度条

时间:2015-04-02 10:23:12      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

#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

技术分享

自定义圆形进度条

标签:

原文地址:http://www.cnblogs.com/thbbsky/p/4386091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!