I'm using xcode 12. I wrote extension UI View as below:
@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
}
}
Things works fine but when I debug view, I saw some purple warnings like this.
x-xcode-debug-views://7f8fd2258020?DBGViewDebuggerLaunchSessionParameter=7f8fd2258020: runtime: Optimization Opportunities: The layer is using dynamic shadows which are expensive to render. If possible try setting
shadowPath
, or pre-rendering the shadow into an image and putting it under the layer.
Can someone explain this to me and help me to get rid of it??
One solution is to "guide" the shadow rendering by setting the shadowPath
explicitly to your needs, eg.:
yourViewWithShadow.layer.shadowPath = UIBezierPath(rect: yourViewWithShadow.bounds).cgPath
Make sure you're setting the frame at the right time!
Another solution is to cache the rasterization:
yourViewWithShadow.layer.shouldRasterize = true
yourViewWithShadow.layer.rasterizationScale = UIScreen.main.scale
Hope this helps you to eliminate expensive calculations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With