1 px边界UIBezierPath(roundedRect:byRoundingCorners :)

问题描述 投票:0回答:1

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

结果有角度宽度问题:

enter image description here

ios swift uibezierpath rounded-corners cashapelayer
1个回答
2
投票

插入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
© www.soinside.com 2019 - 2024. All rights reserved.