【问题标题】:1 px bordered UIBezierPath(roundedRect:byRoundingCorners:)1 px 边框 UIBezierPath(roundedRect:byRoundingCorners:)
【发布时间】:2019-02-23 09:02:27
【问题描述】:

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

结果在角度上有宽度问题:

【问题讨论】:

  • 我看起来可能图层被剪裁到了边界,只显示了边缘的一半。
  • 我在 iOS 10 中测试过,没有任何问题。 imgur.com/a/J7RB7EI
  • @QuocNguyen 好的,可能是由 superview 剪辑引起的。这是一个 UITableViewCell。插入路径的 JonJ 解决方案解决了我的问题。

标签: ios swift uibezierpath rounded-corners cashapelayer


【解决方案1】:

插入 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

【讨论】:

  • 你不会宁愿插入圆角矩形路径而不是形状层的框架吗?
  • 更新答案。
  • clipsToBounds = falsemasksToBounds = false 并没有解决任何问题(可能是由于一些超级视图限制),所以这里的关键是要有一个插图。谢谢。
  • bounds.insetBy(dx: 0.5, dy: 0.5) 效果更好。
  • 您的插图当然取决于您的线宽。 50% 以获得最佳效果。
猜你喜欢
  • 2018-11-17
  • 1970-01-01
  • 2016-10-20
  • 2017-08-04
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多