提问者:小点点

如何显示进度百分比像下面的图像[关闭]


我需要显示像图像的百分比,但没有任何帮助。 有人能帮帮忙吗。 我需要显示完成的进度百分比像所附的图像

现在显示39%完成的图像


共1个答案

匿名用户

谢谢你们的回应伙计们。

我已经创建了视图并添加了基于百分比的标签。

这就是我所做的逻辑。

  1. 获取视图大小并除以10.(例如viewWidth 100)
  2. 然后创建宽度为10(ViewWidth/10)
  3. 的标签
  4. 将标签添加到视图中。 并根据需要更改标签颜色。

这是我做的代码,并为我工作。

class ProgressView : UIView {

var progressFillColor = UIColor.blue
var progressBackgroundColor = UIColor.red
var progressSeparatorColor = UIColor.white
var separatorLblWidth = 2.0

override init(frame: CGRect) {
    super.init(frame: frame)
    self.frame = frame
    self.layer.masksToBounds = true
    self.layer.cornerRadius = 5.0
    self.clipsToBounds = true
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func loadViewWith(Progrees value:CGFloat = 100) {
    self.subviews.forEach({$0.removeFromSuperview()})
    self.backgroundColor = self.progressBackgroundColor
    let progressValue = value > 100 ? value/100.0 : value
    let numberOfLbl = Int(floor(progressValue / 10)) + ((progressValue.truncatingRemainder(dividingBy: 10.0)) > 0 ? 1:0)

    let progressLblWidth = (self.frame.width - CGFloat(9.0 * self.separatorLblWidth))/10
    let percentageWidth = progressLblWidth/10

    for i in 1..<10 { // line Label
        let separatorLabel = UILabel()
        separatorLabel.frame = CGRect.init(x: Double(progressLblWidth * CGFloat(i)) + Double(((i > 0) ? i - 1 : 0) * Int(self.separatorLblWidth)), y: 0.0, width: self.separatorLblWidth, height: Double(self.frame.height))
        separatorLabel.backgroundColor = self.progressSeparatorColor
        self.addSubview(separatorLabel)
    }

    let remaingVlaue = (progressValue.truncatingRemainder(dividingBy: 10.0))
    for i in 0..<numberOfLbl { // fill Label
        let progressLabel = UILabel()
        progressLabel.frame = CGRect.init(x: Double(progressLblWidth * CGFloat(i)) + Double(CGFloat(self.separatorLblWidth) * CGFloat(i)), y: 0.0, width: (i == numberOfLbl - 1 && remaingVlaue != 0) ? Double(remaingVlaue * percentageWidth) : Double(progressLblWidth), height: Double(self.frame.height))
        progressLabel.backgroundColor = self.progressFillColor
        self.addSubview(progressLabel)
    }
}

}

并使用下面的1来调用它。

     let progressView = ProgressView.init(frame: CGRect.init(x: 20, y:20, width: screenRect.size.width - 40, height: 10))

    progressView.loadViewWith(Progrees: 45)

    self.view.addSubview(progressView)