【问题标题】:Fill UIView using a percentage使用百分比填充 UIView
【发布时间】:2019-06-05 12:53:59
【问题描述】:

我有一个来自 API 的 UIView 和百分比数字。我需要让 UIView 根据百分比填充一些颜色。

Here's the UIView

我从另一个问题中得到了基础知识。

class BadgeView: UIView {
    private let fillView = UIView(frame: CGRect.zero)

    private var coeff:CGFloat = 0.5 {
        didSet {
            // Make sure the fillView frame is updated everytime the coeff changes
            updateFillViewFrame()
        }
    }

    override func awakeFromNib() {
        setupView()
    }

    private func setupView() {
        // Setup the layer
        layer.cornerRadius = bounds.height/2.0
        layer.masksToBounds = true

        // Setup filledView backgroundColor and add it as a subview
        fillView.backgroundColor = UIColor(red: 220.0/255.0, green: 220.0/255.0, blue: 220.0/255.0, alpha: 0.4)
        addSubview(fillView)

        // Update fillView frame in case coeff already has a value
        updateFillViewFrame()
    }

    private func updateFillViewFrame() {
        fillView.frame = CGRect(x: 0, y: bounds.height*(1-coeff), width: bounds.width, height: bounds.height*coeff)
    }

    // Setter function to set the coeff animated. If setting it not animated isn't necessary at all, consider removing this func and animate updateFillViewFrame() in coeff didSet
    public func setCoeff(coeff: CGFloat, animated: Bool) {
        if animated {
            UIView.animate(withDuration: 4.0, animations:{ () -> Void in
                self.coeff = coeff
            })
        } else {
            self.coeff = coeff
        }
    }
}

这是返回百分比的函数:

    func cargaOKCheckStatus(resultado: NSDictionary) {
        let JSON = resultado

        if let ElapsedPercentual:Int = JSON.value(forKeyPath: "ResponseEntity.ElapsedPercentual") as? Int {
            porcentaje = ElapsedPercentual
            print(porcentaje)
        }
    }

API 返回 0%、10%、20%、30% 等。所以如果是 10%,UIView 应该用浅灰色填充 10%。现在,它总是半满。

【问题讨论】:

  • 您可以使用setNeedsLayout()setNeedsDisplay() 强制视图刷新UI。

标签: ios swift


【解决方案1】:

几件事:

  • 您可以使用自动布局来简化操作
  • 在创建自定义视图时同时覆盖 init(frame:)init(coder:) 并在这两种方法中调用设置
class BadgeView: UIView {

    private let fillView = UIView(frame: CGRect.zero)
    private var fillHeightConstraint: NSLayoutConstraint!

    private(set) var coeff: CGFloat = 0.0 {
        didSet {
            updateFillViewFrame()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setupView()
    }

    private func setupView() {
        layer.cornerRadius = bounds.height / 2.0
        layer.masksToBounds = true
        fillView.backgroundColor = UIColor(red: 220.0/255.0, green: 220.0/255.0, blue: 220.0/255.0, alpha: 0.4)
        fillView.translatesAutoresizingMaskIntoConstraints = false // ensure autolayout works
        addSubview(fillView)

        // pin view to leading, trailing and bottom to the container view
        fillView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        fillView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        fillView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true

        // save the constraint to be changed later
        fillHeightConstraint = fillView.heightAnchor.constraint(equalToConstant: 0)
        fillHeightConstraint.isActive = true
        updateFillViewFrame()
    }

    private func updateFillViewFrame() {
        fillHeightConstraint.constant = bounds.height * coeff // change the constraint value
        layoutIfNeeded() // update the layout when a constraint changes
    }

    public func setCoeff(coeff: CGFloat, animated: Bool) {
        if animated {
            UIView.animate(withDuration: 4.0, animations:{ () -> Void in
                self.coeff = coeff
            })
        } else {
            self.coeff = coeff
        }
    }

}

这应该可以工作,假设 coeff 是介于 0.01.0 之间的值

【讨论】:

  • 太棒了,谢谢!但它没有做任何事情,我如何传递百分比?
  • 你的print(porcentaje) 打印什么?
  • 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
  • 就像badge.setCoeff(coeff: porcentaje / 100, animated: true),假设您在视图控制器中声明了一个名为badgeBadgeView 实例
  • 我把它放在哪里?因为我把它放在 API if 和 ViewController 的 viewDidLoad 中,它不起作用
猜你喜欢
  • 2017-01-23
  • 2012-01-27
  • 2017-04-16
  • 1970-01-01
  • 2016-04-17
  • 2021-09-19
  • 2013-10-23
  • 2012-02-14
  • 1970-01-01
相关资源
最近更新 更多