iOS:在UIView中以百分比绘制圆圈

Kas*_*hif 2 iphone label uiview ios swift

我有一个UIView圆形的形状,我需要显示UIView百分比值的边框颜色,如果百分比值是50%,它应该填充UIView的半边框颜色.我已经使用了UIBeizer路径addArcWithCenter但是我没有得到完美的解决方案.请帮帮我

gvu*_*sic 9

您可以使用以下代码实现它,只需调整strokeStartstrokeEnd:

    // round view
    let roundView = UIView(frame: CGRectMake(100, 100, 250, 250))
    roundView.backgroundColor = UIColor.whiteColor()
    roundView.layer.cornerRadius = roundView.frame.size.width / 2

    // bezier path
    let 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 * M_PI),
                                  endAngle: CGFloat(1.5 * M_PI),
                                  clockwise: true)
    // circle shape
    let circleShape = CAShapeLayer()
    circleShape.path = circlePath.CGPath
    circleShape.strokeColor = UIColor.redColor().CGColor
    circleShape.fillColor = UIColor.clearColor().CGColor
    circleShape.lineWidth = 1.5
    // set start and end values
    circleShape.strokeStart = 0.0
    circleShape.strokeEnd = 0.8

    // add sublayer
    roundView.layer.addSublayer(circleShape)
    // add subview
    self.view.addSubview(roundView)
Run Code Online (Sandbox Code Playgroud)