我有一个圆形的 UIView
,我需要以百分比值显示 UIView
边框颜色,如果百分比值为 50%,它应该填充一半边框颜色的 UIView。我使用了 UIBeizer
路径 addArcWithCenter
但是我没有得到完美的解决方案。请在这方面帮助我
最佳答案
你可以用下面的代码实现它,只需调整strokeStart
和strokeEnd
:
// 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)
关于iOS:在 UIView 中以百分比形式绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954110/