基于百分比填充笔划颜色,即80%,70%或任何其他颜色

时间:2017-04-26 06:37:50

标签: swift uibezierpath bezier

enter image description here

我创建了一个带有灰色笔触颜色的bezier路径。但是我需要在橙色相同的路径上叠加橙色,并根据要填充的路径百分比(如70%,80%或任何其他值)

    let circlePath = UIBezierPath(arcCenter: CGPoint(x: view.frame.size.width/2,y: view.frame.size.height), radius: CGFloat(25), startAngle: CGFloat(Double.pi), endAngle:CGFloat(0), clockwise: true)
    print(circlePath)
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.cgPath
    shapeLayer.lineDashPattern = [10 ,10]
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.white.cgColor
    shapeLayer.lineWidth = 1.0
    let shapeLayer1 = CAShapeLayer()
    shapeLayer1.path = circlePath.cgPath
    shapeLayer1.fillColor = UIColor.clear.cgColor
    shapeLayer1.strokeColor = UIColor.lightGray.cgColor
    shapeLayer1.lineWidth = 10
    shapeLayer1.addSublayer(shapeLayer)
    view.layer.addSublayer(shapeLayer1)

1 个答案:

答案 0 :(得分:2)

    let completionPercentage = 60.0

    let circlePath = UIBezierPath(arcCenter: CGPoint(x: view.frame.size.width/2,y: view.frame.size.height), radius: CGFloat(25), startAngle: CGFloat(Double.pi), endAngle:CGFloat(0), clockwise: true)
    print(circlePath)
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.cgPath
    shapeLayer.lineDashPattern = [5 ,5]
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.white.cgColor
    shapeLayer.lineWidth = 1.0
    let shapeLayer1 = CAShapeLayer()
    shapeLayer1.path = circlePath.cgPath
    shapeLayer1.fillColor = UIColor.clear.cgColor
    shapeLayer1.strokeColor = UIColor.lightGray.cgColor
    shapeLayer1.lineWidth = 10
    shapeLayer1.addSublayer(shapeLayer)

    let angle = completionPercentage * 1.8

    let circlePath1 = UIBezierPath(arcCenter: CGPoint(x: view.frame.size.width/2,y: view.frame.size.height), radius: CGFloat(25), startAngle: CGFloat(Double.pi), endAngle:CGFloat(((180 + angle)/180)*Double.pi), clockwise: true)
    print(circlePath1)
    let shapeLayer2 = CAShapeLayer()
    shapeLayer2.path = circlePath1.cgPath
    shapeLayer2.lineDashPattern = [5 ,5]
    shapeLayer2.fillColor = UIColor.clear.cgColor
    shapeLayer2.strokeColor = UIColor.white.cgColor
    shapeLayer2.lineWidth = 1.0
    let shapeLayer3 = CAShapeLayer()
    shapeLayer3.path = circlePath1.cgPath
    shapeLayer3.fillColor = UIColor.clear.cgColor
    shapeLayer3.strokeColor = UIColor.orange.cgColor
    shapeLayer3.lineWidth = 10
    shapeLayer3.addSublayer(shapeLayer2)
    view.layer.addSublayer(shapeLayer1)
    view.layer.addSublayer(shapeLayer3)

输出: -

enter image description here