如何将角半径添加到 UIBezier 路径弧
How to add corner radius to a UIBezier Path Arc
我正在使用 UIBezierPath 弧线便捷方法创建弧线。我想要圆弧的末端 - 具体要求请参见附图! roundedRect 版本中使用的 cornerRadius 选项不适用于 arc 方法。任何人都知道如何实现这一目标?提前致谢。 (这与之前提出的问题不同,因为它提供了确切的要求)
let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
let radius: CGFloat = max(bounds.width, bounds.height)
let arcWidth: CGFloat = 10
let startAngle: CGFloat = 4.6 / 3 * π
let endAngle: CGFloat = 4.4 / 3 * π
let path = UIBezierPath(arcCenter: center, radius: radius/2 - arcWidth/2, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.lineWidth = arcWidth
// all thats needed to make the ends rounded is
path.lineCapStyle = .Round
path.stroke()
解决方案
真正起作用的是 lineCap 属性。您可以使用下面的代码。
func drawCircle(view: UIView, startingAngle: CGFloat, endAngle: CGFloat) -> CAShapeLayer {
let path = UIBezierPath(arcCenter: view.center, radius: CGFloat((view.bounds.size.height/2) - 10), startAngle: startingAngle, endAngle:endAngle, clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = UIColor.blackColor().CGColor
shapeLayer.lineWidth = 10.0
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.lineCap = kCALineCapRound
view.layer.addSublayer(shapeLayer)
return shapeLayer
}
结果
只需要添加一行代码:
path.lineCapStyle = .Round.
我正在使用 UIBezierPath 弧线便捷方法创建弧线。我想要圆弧的末端 - 具体要求请参见附图! roundedRect 版本中使用的 cornerRadius 选项不适用于 arc 方法。任何人都知道如何实现这一目标?提前致谢。 (这与之前提出的问题不同,因为它提供了确切的要求)
let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
let radius: CGFloat = max(bounds.width, bounds.height)
let arcWidth: CGFloat = 10
let startAngle: CGFloat = 4.6 / 3 * π
let endAngle: CGFloat = 4.4 / 3 * π
let path = UIBezierPath(arcCenter: center, radius: radius/2 - arcWidth/2, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.lineWidth = arcWidth
// all thats needed to make the ends rounded is
path.lineCapStyle = .Round
path.stroke()
解决方案
真正起作用的是 lineCap 属性。您可以使用下面的代码。
func drawCircle(view: UIView, startingAngle: CGFloat, endAngle: CGFloat) -> CAShapeLayer {
let path = UIBezierPath(arcCenter: view.center, radius: CGFloat((view.bounds.size.height/2) - 10), startAngle: startingAngle, endAngle:endAngle, clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = UIColor.blackColor().CGColor
shapeLayer.lineWidth = 10.0
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.lineCap = kCALineCapRound
view.layer.addSublayer(shapeLayer)
return shapeLayer
}
结果
只需要添加一行代码: path.lineCapStyle = .Round.