我正在使用UIBezierPath为循环进度条制作一个程序是否是一个好的做法?

时间:2017-07-11 08:38:01

标签: swift

观察圆形视图并使其圆形,然后沿着圆形视图绘制圆形路径并给出一个开始笔划并按时间方法给出结束笔划。这是一种正确的做法吗

    var count = 0.0
    var circlePath: UIBezierPath!
    var roundView: UIView!
    var circleShape: CAShapeLayer!
    var x: Timer!
    var headerView: HeaderView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // setup()
        roundView = UIView(frame: CGRect(x: 100, y: 100, width: 300, height: 300))
        roundView.addSubview(label)
        label.centerXAnchor.constraint(equalTo: roundView.centerXAnchor).isActive = true
        label.centerYAnchor.constraint(equalTo: roundView.centerYAnchor).isActive = true
        roundView.layer.cornerRadius = roundView.frame.size.width / 2

        // bezier path
        circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
                                  radius: roundView.frame.size.width / 2,
                                  startAngle: CGFloat(-0.5 * Double.pi),
                                  endAngle: CGFloat(1.5 * Double.pi),
                                  clockwise: true)
        // circle shape
        circleShape = CAShapeLayer()
        circleShape.path = circlePath.cgPath
        circleShape.borderWidth = 10
        circleShape.strokeColor = UIColor.blue.cgColor
        circleShape.fillColor = UIColor.clear.cgColor
        circleShape.lineWidth = 10
        // set start and end values
        circleShape.strokeStart = 0.0
        x = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)


    }

添加结束笔划

@objc func update() {
        // Something cool
        if count >= 1{
            x.invalidate()

        }
        count = count + 0.01
        self.label.text = String(Int(count * 100))
        circleShape.strokeEnd = CGFloat(count)
        roundView.layer.addSublayer(circleShape)
        // add subview
        self.tableView.addSubview(roundView)
        print(count)
    }

1 个答案:

答案 0 :(得分:0)

当我们谈论好的或坏的做法时,通常意味着这种方法是否有效,安全与否。说做力展开是不好的做法,使用if let是一种很好的做法。

在您的情况下,圆形进度条只不过是顶部有一些图层或路径的视图,最终形成了条状视图。这是正确的做法,这不是一个坏习惯。