自定义循环进度视图IOS

时间:2014-04-10 12:58:24

标签: ios objective-c uibezierpath

我正在尝试创建一个使用计时器更新的循环进度视图。我试图得到4个不同颜色的部分。我已设法用一种颜色更新圆圈,但我无法弄清楚如何将其分成4个四分之一圆圈。

@implementation CircularProgressBar
- (void)drawRect:(CGRect)rect
{

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];

    // Create our arc, with the correct angles
    [bezierPath addArcWithCenter:CGPointMake(160, 180)
                          radius:120
                      startAngle:0
                        endAngle: DEGREES_TO_RADIANS(360)
                       clockwise:YES];

    // Set the display for the path, and stroke it
    bezierPath.lineWidth = 12;
    [[UIColor whiteColor] setStroke];
    [bezierPath stroke];

    UIBezierPath* progressPath = [UIBezierPath bezierPath];

    // Create our arc, with the correct angles
    [progressPath addArcWithCenter:CGPointMake(160, 180)
                          radius:120
                      startAngle:-M_PI_2
                        endAngle:-M_PI_2 + (2/M_PI) * self.progress
                       clockwise:YES];

    // Set the display for the path, and stroke it
    progressPath.lineWidth = 12;
    [[UIColor redColor] setStroke];
    [progressPath stroke];

    UIBezierPath* progressPath1 = [UIBezierPath bezierPath];

    // Create our arc, with the correct angles
    [progressPath1 addArcWithCenter:CGPointMake(160, 180)
                            radius:120
                        startAngle:-M_PI_2 + (2/M_PI)
                          endAngle:M_PI_2* self.progress1/
                         clockwise:YES];

    // Set the display for the path, and stroke it
    progressPath1.lineWidth = 12;


    [[UIColor grayColor] setStroke];
        [progressPath1 stroke];

}

-(void)setProgress:(CGFloat)progress {
    //update progress with timer  
    if (progress != _progress) {
        _progress = progress;
        if(progress > 1.1)
            _progress1=_progress;
        [self setNeedsDisplay];
    }

}

计时器类

-(float) progress {
    return (float)self.count/450.0; 
}

计时器是1800秒。除以4是每节450秒

2 个答案:

答案 0 :(得分:4)

我为此目的创建了一个小型库,非常灵活,有很多选项:https://github.com/kirualex/KAProgressLabel

一个简单的配置如下:

[self.pLabel setTrackWidth: 2.0];
[self.pLabel setProgressWidth: 4];
self.pLabel.fillColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.3];
self.pLabel.trackColor = self.startSlider.tintColor;
self.pLabel.progressColor = [UIColor greenColor];

答案 1 :(得分:2)

您可以在链接http://code4app.net/category/progress上下载有用且非常容易嵌入的progressview类型