ios - UIBezierPath roundedRect 有奇怪的间隙

标签 ios swift uikit core-graphics

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


enter image description here

最佳答案

我怀疑圆角矩形是通过将一些曲线连接在一起来实现的,并且您看到的伪像是由图层的 lineJoin 属性引起的。

lineJoin 设置为 .round 为我解决了这个问题:

layer.lineJoin = .round

enter image description here

关于ios - UIBezierPath roundedRect 有奇怪的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76214705/

相关文章:

ios - UITableViewCell 对 indexPath 的更改不正确

ios - 在 UIAlertView 中更改按钮标题颜色

android - 从 Android 和 iOS 图像中提取剪影,最好的选择? OpenCV、OpenGL、其他……?

iOS:当拖动到指定区域时如何仅确定一次按钮的位置

ios - 在 swift 4 中返回之前强制等待 Alamofire 响应

ios - 在 UITableViewCell 中委托(delegate) UICollectionView

swift - UInt32 数组到 Swift 中的字符串字节数组

ios - 在 if 语句中使用 where 子句

uikit - 如何将 UIImage 转换或加载到 PKDrawing 中?

ios - UITableViewCell 自定义编辑 View