iOS 圆形进度条

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

你可能感兴趣的:(iOS 圆形进度条)