UIBezierPath roundedRect 有奇怪的间隙

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

我需要画一个圆角矩形。然而,在非常特殊的角半径值(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)
        
    }
}


ios swift uikit core-graphics
1个回答
1
投票

我怀疑圆角矩形是通过将一些曲线连接在一起实现的,而您看到的伪像是由图层的

lineJoin
属性引起的。

设置

lineJoin
.round
为我修复它:

layer.lineJoin = .round

© www.soinside.com 2019 - 2024. All rights reserved.