.m文件
@implementation CircularProgressView
- (id)initWithFrame:(CGRect)frame
backColor:(UIColor *)backColor
progressColor:(UIColor *)progressColor
lineWidth:(CGFloat)lineWidth
totalTime:(CGFloat)totalTime
type:(CircularProgressViewType)type;
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
_backColor = backColor;
_progressColor = progressColor;
_lineWidth = lineWidth;
_timerFloat = totalTime;
_currentProgress = 61.0f;
_timeWhole = 61;
isStart = YES;
if (type == CircularProgressViewType_Label)
{
_timeLabel = [[UILabel alloc]init];
_timeLabel.font = [UIFont systemFontOfSize:20.0f];
_timeLabel.textAlignment = NSTextAlignmentCenter;
_timeLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
_timeLabel.text = @"01:01";
[self addSubview:_timeLabel];
}
else
{
_stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
_stopButton.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[_stopButton setTitle:@"Stop" forState:UIControlStateNormal];
[_stopButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_stopButton addTarget:self action:@selector(stopClicked) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_stopButton];
}
}
return self;
}
- (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];
NSLog(@"self.progress1111-->%f",self.progress);
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)updateProgressCircle{
// NSLog(@"updateProgressCircle");
self.currentProgress -= 1;
self.timeWhole -= 1;
//update progress value
self.progress = (float) (_currentProgress / _timerFloat);
//redraw back & progress circles
NSLog(@"self.progress-->%f",self.progress);
[self setNeedsDisplay];
if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressDelegate)])
{
[self.delegate didUpdateProgressView:self.currentProgress];
}
NSString *str_minute = [NSString stringWithFormat:@"%02ld", (_timeWhole % 3600) / 60];
NSString *str_second = [NSString stringWithFormat:@"%02ld", _timeWhole % 60];
NSString *format_time = [NSString stringWithFormat:@"%@ : %@", str_minute, str_second];
// 修改倒计时标签及显示内容
self.timeLabel.text = format_time;
if (self.progress <= 0.0f)
{
//invalid timer
[self.timer invalidate];
self.currentProgress = 61.0f;
self.timeWhole = 61;
//restore progress value
// self.progress = 0;
//self redraw
// [self setNeedsDisplay];
}
}
- (void)stopClicked
{
if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressDelegate)])
{
[self.delegate stopEvent];
}
}
- (void)play{
if (isStart)
{
NSLog(@"play");
self.timer = [NSTimer scheduledTimerWithTimeInterval:TIMER target:self selector:@selector(updateProgressCircle) userInfo:nil repeats:YES];
[self.timer fire];
isStart = NO;
}
}
- (void)pause{
if (!isStart)
{
[self.timer invalidate];
isStart = YES;
}
}
- (void)revert{
isStart = YES;
[self updateProgressCircle];
self.progress = 61;
self.currentProgress = 61.0f;
self.timeWhole = 61;
_timeLabel.text = @"01:01";
[self.timer invalidate];
}