如何为带有不同圆角的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)

结果在角度上存在宽度问题:

ios - 1像素边框UIBezierPath(roundedRect:byRoundingCorners :)-LMLPHP

最佳答案

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

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

关于ios - 1像素边框UIBezierPath(roundedRect:byRoundingCorners :),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52401596/

10-12 03:31