ios – Configure Label and Round Progress Bar in Init vs layoutSubviews

0
98


I am engaged on a pace take a look at app which has a customized CircularProgressView that features a speedLabel and the speedLabel is centered within the progress view.

I’ve a createCircularPath methodology to create the progress view’s path and I name it within layoutSubviews.

Once I configure the speedLabel within the customized view’s init and run the pace take a look at then the speedLabel textual content and the progressTrack is updating as anticipated because the take a look at is operating. But when I configure the speedLabel in layoutSubviews along with the round path than neither of them are being up to date.

I’ve tried enjoying with it by altering the z-scores. I’ve additionally tried embedding the round path, progressLayer and trackLayer inside one other view then including the view and label as subviews, however nonetheless the progress and pace will not be being up to date through the pace take a look at when configured contained in the layoutSubviews.

Searching for a proof as to why that is.

Display captures of working progress (when speedLabel configured in init):
enter image description here
enter image description here

And when when speedLabel configured in layoutSubviews the label and progress simply keep static. Beneath is code when each configured in layoutSubviews

import UIKit

class CircularProgressView: UIView {
    
    var progressLayer = CAShapeLayer()
    var trackLayer = CAShapeLayer()
    var speedLabel = UILabel()
    
    override init(body: CGRect) {
        tremendous.init(body: body)
    }
    
    required init?(coder: NSCoder) {
        tremendous.init(coder: coder)
    }
    
    override func layoutSubviews() {
        tremendous.layoutSubviews()
        createCircularPath()
        configureSpeedLabel()
    }
    
    var progressColor = UIColor.white {
        didSet {
            progressLayer.strokeColor = progressColor.cgColor
        }
    }
    
    var trackColor = UIColor.white {
        didSet {
            trackLayer.strokeColor = trackColor.cgColor
        }
    }
    
    var pace = 0 {
        didSet {
            speedLabel.textual content = String(pace)
        }
    }
    
    fileprivate func configureSpeedLabel() {
        speedLabel.translatesAutoresizingMaskIntoConstraints = false
        speedLabel.textual content = String(pace)
        speedLabel.font = UIFont(identify: "TimesNewRomanPSMT", measurement: 24)
        speedLabel.textColor = UIColor.black
        speedLabel.isOpaque = true
        self.addSubview(speedLabel)
        NSLayoutConstraint.activate([
            speedLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            speedLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor)
        ])
    }
    fileprivate func createCircularPath() {
        self.backgroundColor = UIColor.clear
        self.layer.cornerRadius = self.body.width/2
        let path = UIBezierPath(arcCenter: CGPoint(x: body.width/2, y: body.top/2), radius: (body.width - 1.5)/2, startAngle: CGFloat(-0.5 * .pi), endAngle: CGFloat(1.5 * .pi), clockwise: true)
        trackLayer.path = path.cgPath
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.strokeColor = trackColor.cgColor
        trackLayer.opacity = 0.3
        trackLayer.lineWidth = 10.0
        trackLayer.strokeEnd = 1.0
        self.layer.addSublayer(trackLayer)

        progressLayer.path = path.cgPath
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = progressColor.cgColor
        progressLayer.lineWidth = 10.0
        progressLayer.strokeEnd = 0.0
        self.layer.addSublayer(progressLayer)
    }

}