1 px 边框 UIBezierPath(roundedRect:byRoundingCorners:)

1 px bordered UIBezierPath(roundedRect:byRoundingCorners:)

如何为具有不同圆角的 UIBezierPath 获得干净的 1 px 边框?

在下面的示例中,我使用了 3 个角。代码在 UIView:

let borderLayer = CAShapeLayer()
borderLayer.frame = bounds
borderLayer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topRight, .bottomLeft, .bottomRight], cornerRadii: CGSize(width: 24, height: 24)).cgPath
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.lineWidth = 1
borderLayer.strokeColor = UIColor.white.cgColor
layer.addSublayer(borderLayer)

结果存在角度宽度问题:

插入 1 个像素,或在 UIView 中设置 clipsToBounds = false

let insetBounds = bounds.insetBy(dx: 1.0, dy: 1.0)
borderLayer.path = UIBezierPath(roundedRect: insetBounds, byRoundingCorners: [.topRight, .bottomLeft, .bottomRight], cornerRadii: CGSize(width: 24, height: 24)).cgPath