Tha*_*ran 14 debugging xcode uiview swift
我正在使用 xcode 12。我编写了扩展 UI 视图,如下所示:
@IBInspectable
public var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
@IBInspectable
public var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set {
layer.shadowOpacity = newValue
}
}
@IBInspectable
public var shadowOffset: CGSize {
get {
layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
@IBInspectable
public var shadowColor: UIColor {
get {
return UIColor(cgColor: layer.shadowColor ?? UIColor.clear.cgColor)
}
set {
layer.shadowColor = newValue.cgColor
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但是当我调试视图时,我看到了一些像这样的紫色警告。
x-xcode-debug-views://7f8fd2258020?DBGViewDebuggerLaunchSessionParameter=7f8fd2258020:运行时:优化机会:该层正在使用动态阴影,渲染成本很高。如果可能,请尝试设置
shadowPath或将阴影预渲染为图像并将其放在图层下方。
有人可以向我解释这一点并帮助我摆脱它吗?

cho*_*fie 12
一种解决方案是通过shadowPath根据您的需要明确设置来“引导”阴影渲染,例如:
yourViewWithShadow.layer.shadowPath = UIBezierPath(rect: yourViewWithShadow.bounds).cgPath
Run Code Online (Sandbox Code Playgroud)
确保您在正确的时间设置框架!
另一种解决方案是缓存光栅化:
yourViewWithShadow.layer.shouldRasterize = true
yourViewWithShadow.layer.rasterizationScale = UIScreen.main.scale
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助您消除昂贵的计算。