Ios 该层被一个路径为'的“CAShapeLayer”屏蔽;这是一个矩形,一个圆形的矩形,或者一个椭圆
我正在尝试创建一个视图,每个角都可以有不同的值。一切正常,但在调试模式下,我收到优化机会警告,警告如下: 该层被路径为矩形、圆形矩形或椭圆的Ios 该层被一个路径为'的“CAShapeLayer”屏蔽;这是一个矩形,一个圆形的矩形,或者一个椭圆,ios,swift,uikit,calayer,Ios,Swift,Uikit,Calayer,我正在尝试创建一个视图,每个角都可以有不同的值。一切正常,但在调试模式下,我收到优化机会警告,警告如下: 该层被路径为矩形、圆形矩形或椭圆的CAShapeLayer遮罩。相反,使用适当变换的容器层,设置corneradius和masksToBounds 我想知道是否有更好的方法来达到同样的效果,以下是我当前的代码: extension UIBezierPath { convenience init( shouldRoundRect rect: CGRect,
CAShapeLayer
遮罩。相反,使用适当变换的容器层,设置corneradius
和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
}
你可能想检查一下:这只是一个建议。如果没有任何性能问题,请忽略它。