Making UIProgressView Rounded corners

前端 未结 13 1684
旧巷少年郎
旧巷少年郎 2021-02-07 13:59

I have created a UIProgressView with following properties

progressView.progressTintColor = UIColor.appChallengeColorWithAlpha(1.0)
progressView.trac         


        
13条回答
  •  遇见更好的自我
    2021-02-07 14:39

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let v = ProgessView(frame: CGRect(x: 20, y: 200, width: 100, height: 10))
            view.addSubview(v)
    
            //v.progressLayer.strokeEnd = 0.8
    
        }
    }
    
    class ProgessView: UIView {
    
        lazy var progressLayer: CAShapeLayer = {
            let line = CAShapeLayer()
            let path = UIBezierPath()
            path.move(to: CGPoint(x: 5, y: 5))
            path.addLine(to: CGPoint(x: self.bounds.width - 5, y: 5))
            line.path = path.cgPath
            line.lineWidth = 6
            line.strokeColor = UIColor(colorLiteralRed: 127/255, green: 75/255, blue: 247/255, alpha: 1).cgColor
            line.strokeStart = 0
            line.strokeEnd = 0.5
            line.lineCap = kCALineCapRound
            line.frame = self.bounds
            return line
        }()
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.white
            layer.cornerRadius = 5
            layer.borderWidth = 1
            layer.borderColor = UIColor(colorLiteralRed: 197/255, green: 197/255, blue: 197/255, alpha: 1).cgColor
            layer.addSublayer(progressLayer)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
    }
    

    Test my codes. You can design the height and the width as your want. You can use strokeEnd to change the progress of the progressView. You can add an animation to it. But actually, it is already animatable, you can change the value of the strokeEnd to see its primary effect. If you want to design your own animation. Try CATransaction like below.

    func updateProgress(_ progress: CGFloat) {
            CATransaction.begin()
            CATransaction.setAnimationDuration(3)
            progressLayer.strokeEnd = progress
            CATransaction.commit()
        }
    

提交回复
热议问题