一个简单的动态环形进度条
实现:
- 1首先创建一个ProgressView 的View 类
- 2重写progress的set方法
- (void)setProgress:(CGFloat)progress
{
_progress = progress;
[self setNeedsDisplay];
}
- 3在drawRect中实现
CGPoint centerP = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = self.bounds.size.width * 0.5 - 10;
CGFloat startA = - M_PI_2;
CGFloat endA = -M_PI_2 + self.progress * M_PI * 2;
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:startA endAngle:endA clockwise:YES];
[[UIColor purpleColor] set];
path.lineWidth = 10.0;
// path.lineJoinStyle = kCGLineJoinRound;
path.lineCapStyle = kCGLineCapRound;
[path stroke];
这里只是画一个环形,弧度根据progress的数值来定
- 4在显示的页面中加入你创建的ProgressView视图,在实现的地方写入
//初始为55.32%
self.startF = 0.00;
self.endF = 55.32;
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeProgress)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
self.link = link;
数值根据需求来定
- 5最后实现changeProgress动态改变
- (void)changeProgress
{
if (_startF>= _endF) {
self.ProgressView.progress = _endF * 0.01;
self.textLabel.text = [NSString stringWithFormat:@"%.2f%%",_endF];
[self.link invalidate];
}else{
self.ProgressView.progress = _startF * 0.01;
self.textLabel.text = [NSString stringWithFormat:@"%.2f%%",_startF];
}
_startF = _startF * 100 + 50;
_startF = _startF * 0.01;
}
只是一个普通的实现..
github: https://github.com/SpTTlv/dynamicsProgress