如何圆 UIBezierPath 线边缘?
How to round UIBezierPath line edge?
我有一个 UIBezierPath 的抽屉
let line = CAShapeLayer()
line.path = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: CGFloat(114.0/180.0) * .pi,
endAngle: .pi,
clockwise: true).cgPath
line.lineWidth = LINE_WIDTH
line.strokeColor = UIColor.red.withAlphaComponent(0.9).cgColor
line.fillColor = UIColor.clear.cgColor
我尝试通过添加一些新的 CAShapeLayer() 来圆角
let corner = CAShapeLayer()
corner.path = UIBezierPath(arcCenter: coordByCorner(114.0*1.00035),
radius: LINE_WIDTH/2,
startAngle: CGFloat(114.0/180.0) * .pi,
endAngle: CGFloat(114.0/180.0 + 1) * .pi,
clockwise: false).cgPath
corner.strokeColor = UIColor.clear.cgColor
corner.fillColor = UIColor.red.withAlphaComponent(0.9).cgColor
line.addSublayer(corner)
但我认为这是非常糟糕的变体,当我更改图层的颜色时,颜色会随着时间间隔的不同而变化。
此外,这一切看起来像这样:
P.S.: 1.00035 用于消除层与层之间的间隙。并且 alpha 需要 < 1.0 (0.1 - 0.9),那么它的不透明性如何?
一个简单的方法是创建向内移动所需 cornerRadius
的路径,用两倍粗的线描边并应用圆线连接样式。
let layer = CAShapeLayer()
layer.strokeColor = UIColor.lightGray.cgColor
layer.fillColor = UIColor.lightGray.cgColor
layer.lineWidth = 2.0 * cornerRadius
layer.lineJoin = kCALineJoinRound
layer.path = getSemicirclePath(
arcCenter: arcCenter,
radius: radius,
cornerRadius: cornerRadius
)
func getSemicirclePath(arcCenter: CGPoint, radius: CGFloat, cornerRadius: CGFloat) -> CGPath {
let path = UIBezierPath(
arcCenter: CGPoint(x: arcCenter.x, y: arcCenter.y - cornerRadius),
radius: radius - cornerRadius,
startAngle: .pi,
endAngle: 2.0 * .pi,
clockwise: true
)
path.close()
return path.cgPath
}
示例结果如下:
添加kCALineCapRound解决了问题
let line = CAShapeLayer()
line.path = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: CGFloat(114.0/180.0) * .pi,
endAngle: .pi,
clockwise: true).cgPath
line.lineWidth = LINE_WIDTH
line.strokeColor = UIColor.red.withAlphaComponent(0.9).cgColor
line.fillColor = UIColor.clear.cgColor
line.lineCap = kCALineCapRound // this parameter solve my problem
我有一个 UIBezierPath 的抽屉
let line = CAShapeLayer()
line.path = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: CGFloat(114.0/180.0) * .pi,
endAngle: .pi,
clockwise: true).cgPath
line.lineWidth = LINE_WIDTH
line.strokeColor = UIColor.red.withAlphaComponent(0.9).cgColor
line.fillColor = UIColor.clear.cgColor
我尝试通过添加一些新的 CAShapeLayer() 来圆角
let corner = CAShapeLayer()
corner.path = UIBezierPath(arcCenter: coordByCorner(114.0*1.00035),
radius: LINE_WIDTH/2,
startAngle: CGFloat(114.0/180.0) * .pi,
endAngle: CGFloat(114.0/180.0 + 1) * .pi,
clockwise: false).cgPath
corner.strokeColor = UIColor.clear.cgColor
corner.fillColor = UIColor.red.withAlphaComponent(0.9).cgColor
line.addSublayer(corner)
但我认为这是非常糟糕的变体,当我更改图层的颜色时,颜色会随着时间间隔的不同而变化。
此外,这一切看起来像这样:
P.S.: 1.00035 用于消除层与层之间的间隙。并且 alpha 需要 < 1.0 (0.1 - 0.9),那么它的不透明性如何?
一个简单的方法是创建向内移动所需 cornerRadius
的路径,用两倍粗的线描边并应用圆线连接样式。
let layer = CAShapeLayer()
layer.strokeColor = UIColor.lightGray.cgColor
layer.fillColor = UIColor.lightGray.cgColor
layer.lineWidth = 2.0 * cornerRadius
layer.lineJoin = kCALineJoinRound
layer.path = getSemicirclePath(
arcCenter: arcCenter,
radius: radius,
cornerRadius: cornerRadius
)
func getSemicirclePath(arcCenter: CGPoint, radius: CGFloat, cornerRadius: CGFloat) -> CGPath {
let path = UIBezierPath(
arcCenter: CGPoint(x: arcCenter.x, y: arcCenter.y - cornerRadius),
radius: radius - cornerRadius,
startAngle: .pi,
endAngle: 2.0 * .pi,
clockwise: true
)
path.close()
return path.cgPath
}
示例结果如下:
添加kCALineCapRound解决了问题
let line = CAShapeLayer()
line.path = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: CGFloat(114.0/180.0) * .pi,
endAngle: .pi,
clockwise: true).cgPath
line.lineWidth = LINE_WIDTH
line.strokeColor = UIColor.red.withAlphaComponent(0.9).cgColor
line.fillColor = UIColor.clear.cgColor
line.lineCap = kCALineCapRound // this parameter solve my problem