我需要绘制一个圆角矩形。然而,在非常特殊的角半径值(34 到 37 之间)时,它会导致非常奇怪的错误,如下图所示。有人可以向我解释一下为什么会这样吗?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
drawView()
self.view.backgroundColor = .red
}
func drawView() {
let rect = CGRect(x: 100, y: 100, width: 200, height: 100)
let borderWidth = 10.0
let cornerRadius = 34.0
let layer = CAShapeLayer()
layer.strokeColor = UIColor.green.cgColor
layer.lineWidth = borderWidth
layer.fillColor = UIColor.white.cgColor
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
layer.path = path.cgPath
self.view.layer.addSublayer(layer)
}
}
最佳答案
我怀疑圆角矩形是通过将一些曲线连接在一起来实现的,并且您看到的伪像是由图层的 lineJoin
属性引起的。
将 lineJoin
设置为 .round
为我解决了这个问题:
layer.lineJoin = .round
关于ios - UIBezierPath roundedRect 有奇怪的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76214705/