自定义进度视图

时间:2015-12-22 16:39:04

标签: ios swift uiview uiprogressview

我想为我的iOS应用做一个自定义进度视图,有2个点。这是我的代码:

import UIKit

@IBDesignable
class StepProgressView: UIView {

    @IBInspectable var progress: Float = 0

    var progressColor = UIColor.blackColor()
    var bgColor = UIColor.whiteColor()

    override func layoutSubviews() {
        self.backgroundColor = UIColor.clearColor()
    }

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        let height = frame.height-8

        let circle1 = UIView(frame: CGRect(x: frame.width*(1/3), y: 0, width: frame.height, height: frame.height))
        circle1.backgroundColor = bgColor
        circle1.layer.cornerRadius = frame.height/2
        addSubview(circle1)

        let circle2 = UIView(frame: CGRect(x: frame.width*(2/3), y: 0, width: frame.height, height: frame.height))
        circle2.backgroundColor = bgColor
        circle2.layer.cornerRadius = frame.height/2
        addSubview(circle2)

        let bgView = UIView(frame: CGRect(x: height/2, y: 4, width: frame.width-height/2, height: height))
        bgView.backgroundColor = bgColor
        bgView.layer.cornerRadius = height/2
        addSubview(bgView)

        let progressView = UIView(frame: CGRect(x: 0, y: 4, width: frame.width*CGFloat(progress), height: height))
        progressView.backgroundColor = progressColor
        progressView.layer.cornerRadius = height/2
        addSubview(progressView)
    }

}

结果:

Result

然而,正如您所看到的,圈子并没有被填满"当进展超过其中一个时,我不知道该怎么做。我可以创建另一个视图,但我不知道如何处理角半径。

你能帮助我吗?

由于

0 个答案:

没有答案