ios - 该层被“CAShapeLayer”所掩盖,路径为矩形、圆角矩形或椭圆
问题描述
我正在尝试创建一个可以为每个角具有不同值的视图。一切都按预期工作,但在Vide Debug模式下,我收到一个优化机会警告,上面写着:
该层被 a 所掩盖,CAShapeLayer
路径为矩形、圆角矩形或椭圆。相反,使用适当转换的容器层cornerRadius
和masksToBounds
设置。
我想知道是否有更好的方法来达到相同的效果,这是我当前的代码:
extension UIBezierPath {
convenience init(
shouldRoundRect rect: CGRect,
topLeftRadius: CGFloat,
topRightRadius: CGFloat,
bottomLeftRadius: CGFloat,
bottomRightRadius: CGFloat
){
self.init()
let path = CGMutablePath()
let topLeft = rect.origin
let topRight = CGPoint(x: rect.maxX, y: rect.minY)
let bottomRight = CGPoint(x: rect.maxX, y: rect.maxY)
let bottomLeft = CGPoint(x: rect.minX, y: rect.maxY)
if topLeftRadius != 0 {
path.move(to: CGPoint(x: topLeft.x + topLeftRadius, y: topLeft.y))
} else {
path.move(to: topLeft)
}
if topRightRadius != 0 {
path.addLine(to: CGPoint(x: topRight.x - topRightRadius, y: topRight.y))
path.addArc(tangent1End: topRight, tangent2End: CGPoint(x: topRight.x, y: topRight.y + topRightRadius), radius: topRightRadius)
} else {
path.addLine(to: topRight)
}
if bottomRightRadius != 0 {
path.addLine(to: CGPoint(x: bottomRight.x, y: bottomRight.y - bottomRightRadius))
path.addArc(tangent1End: bottomRight, tangent2End: CGPoint(x: bottomRight.x - bottomRightRadius, y: bottomRight.y), radius: bottomRightRadius)
} else {
path.addLine(to: bottomRight)
}
if bottomLeftRadius != 0 {
path.addLine(to: CGPoint(x: bottomLeft.x + bottomLeftRadius, y: bottomLeft.y))
path.addArc(tangent1End: bottomLeft, tangent2End: CGPoint(x: bottomLeft.x, y: bottomLeft.y - bottomLeftRadius), radius: bottomLeftRadius)
} else {
path.addLine(to: bottomLeft)
}
if topLeftRadius != 0 {
path.addLine(to: CGPoint(x: topLeft.x, y: topLeft.y + topLeftRadius))
path.addArc(tangent1End: topLeft, tangent2End: CGPoint(x: topLeft.x + topLeftRadius, y: topLeft.y), radius: topLeftRadius)
} else {
path.addLine(to: topLeft)
}
path.closeSubpath()
cgPath = path
}
}
class CornerRadiusView: UIView {
private var topLeftRadius: CGFloat = 0
private var topRightRadius: CGFloat = 0
private var bottomLeftRadius: CGFloat = 0
private var bottomRightRadius: CGFloat = 0
func setCornerRadius(radius: CornerRadius) {
topLeftRadius = radius.topLeft
topRightRadius = radius.topRight
bottomLeftRadius = radius.bottomLeft
bottomRightRadius = radius.bottomRight
setNeedsLayout()
}
override open func layoutSubviews() {
super.layoutSubviews()
applyRadiusMask()
}
private func applyRadiusMask() {
let path = UIBezierPath(
shouldRoundRect: bounds,
topLeftRadius: topLeftRadius,
topRightRadius: topRightRadius,
bottomLeftRadius: bottomLeftRadius,
bottomRightRadius: bottomRightRadius
)
let shape = CAShapeLayer()
shape.path = path.cgPath
layer.mask = shape
}
}
struct CornerRadius {
let topLeft: CGFloat
let topRight: CGFloat
let bottomLeft: CGFloat
let bottomRight: CGFloat
}
解决方案
推荐阅读
- json - wso2 使用数组将 xml 转换为 json
- excel - 尝试在 VBA 中创建一个用户表单,该用户表单返回一组基于现有电子表格中数据的标准指定的值
- swift - 使用 PDFKit 删除高亮注释
- selenium - selenium & chrome 78 未能通过基本身份验证
- c# - 使用默认值更改 SQL Server 用户定义类型仍然强制我传递新列
- mobile - Ubuntu 移动宽带
- python - python find index of elements functionally depending on a condition
- swift - 禁用特定表格视图单元格的按钮功能
- excel - 如何将别名设置为发件人帐户?
- html - 如何将图像与右侧的段落对齐