1,利用drawRect画两个圆
- (void)drawRect:(CGRect)rect
{
//draw background circle
UIBezierPath *backCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2,self.bounds.size.height / 2)
radius:self.bounds.size.width / 2 - self.lineWidth / 2
startAngle:(CGFloat) - M_PI_2
endAngle:(CGFloat)(1.5 * M_PI)
clockwise:YES];
[self.backColor setStroke];
backCircle.lineWidth = self.lineWidth;
[backCircle stroke];
if (self.progress != 0)
{
//draw progress circle
UIBezierPath *progressCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2,self.bounds.size.height / 2)
radius:self.bounds.size.width / 2 - self.lineWidth / 2
startAngle:(CGFloat) - M_PI_2
endAngle:(CGFloat)(- M_PI_2 + self.progress * 2 * M_PI)
clockwise:YES];
[self.progressColor setStroke];
progressCircle.lineWidth = self.lineWidth;
[progressCircle stroke];
}
}
开始
- (void)play{
if (isStart)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:TIMER target:self selector:@selector(updateProgressCircle) userInfo:nil repeats:YES];
[self.timer fire];
isStart = NO;
}
}
- (void)updateProgressCircle{
self.currentProgress += 0.1f;
//update progress value
self.progress = (float) (_currentProgress / _timerFloat);
//redraw back & progress circles
[self setNeedsDisplay];
if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressDelegate)])
{
[self.delegate didUpdateProgressView:self.currentProgress];
}
_timeLabel.text = [NSString stringWithFormat:@"%.0f",self.currentProgress];
if (self.progress >= 1.0f)
{
//invalid timer
[self.timer invalidate];
self.currentProgress = 0.0f;
//restore progress value
// self.progress = 0;
//self redraw
// [self setNeedsDisplay];
}
}
暂停
- (void)pause{
if (!isStart)
{
[self.timer invalidate];
isStart = YES;
}
}
重置
- (void)revert{
isStart = YES;
[self updateProgressCircle];
self.progress = 0;
self.currentProgress = 0.0f;
_timeLabel.text = @"0";
[self.timer invalidate];
}
git 地址
https://github.com/W-King/CircularProgress
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。