ios - 1 px 边框 UIBezierPath(roundedRect :byRoundingCorners:)

标签 ios swift uibezierpath rounded-corners cashapelayer

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

最佳答案

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

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 px 边框 UIBezierPath(roundedRect :byRoundingCorners:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52401596/

相关文章:

ios - 为 Alamofire 创建请求主体

svg - 调整 PaintCode 生成的路径大小

ios - #选择器作为参数?

objective-c - iOS - 我是否犯了一个愚蠢的 if 语句错误?我想我失去了情节

ios - 设置 UISearchBar 的搜索字段背景图像会更改填充

ios - 如何计算弯曲的 SKSpriteNode 的 "center"点?

ios - 从远程位置加载 CGpath/UIBezierPath

ios - Delphi XE6 - 我无法将应用程序发送到 Apple Store

ios - 如何在不使用 Swift4 中的 objective-c 的情况下向标签添加操作

ios - Swift、Firebase 从未知子项中检索数据