我已使用以下代码将UIBezerPath
添加到UIView
。
extension CGFloat {
func toRadians() -> CGFloat {//0 360
return self * CGFloat(Double.pi) / 180.0
}
}
override func viewDidAppear(_ animated: Bool) {
let trackLayer = CAShapeLayer()
let circularPath = UIBezierPath(arcCenter: CGPoint(x: arcView.frame.size.width/2,
y: arcView.frame.size.height),
radius: arcView.frame.size.height/2,
startAngle: CGFloat(180.0).toRadians(),
endAngle: CGFloat(0.0).toRadians(),
clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.red.cgColor
trackLayer.lineWidth = 3
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = kCALineCapButt
arcView.layer.sublayers?.removeAll()
arcView.layer.addSublayer(trackLayer)
}
但问题是它在顶部提供额外的不需要的空间,这是我不需要的。我尝试将各种值设置为circularPath
,但未按要求设置。
答案 0 :(得分:0)
以下是您的代码: -
let arcCenter = CGPoint(x: myview.frame.size.width / 2, y: myview.frame.size.height / 2)
let circleRadius = myview.frame.size.width / 2
let circlePath = UIBezierPath(arcCenter: arcCenter, radius: circleRadius, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 2, clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.lineWidth = 5
myview.layer.addSublayer(shapeLayer)
// Make the view color transparent
myview.backgroundColor = UIColor.clear
<强>输出: - 强>
希望它有帮助。!!