Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALayer - Shadow causes a performance hit?

So I am doing some custom animations on my navigationcontroller and the way it pushes and pops the viewControllers.

Everything runs smooth. As soon as I add the following code (In a subclass of UINavigationController), I face a huge performance hit. After adding a shadow all animations become very laggy. Is this expected or am I doing something wrong in the code?

// This code gets called once during NavigationController initialization. [self.view setClipsToBounds:NO]; [self.view.layer setCornerRadius:5]; [self.view.layer setShadowOffset:CGSizeMake(0, 20)]; [self.view.layer setShadowColor:[[UIColor yellowColor] CGColor]]; [self.view.layer setShadowRadius:20.0]; [self.view.layer setShadowOpacity:1]; 

EDIT:

Changed my shadow radius to 1 and it's still slow

like image 803
aryaxt Avatar asked Apr 03 '12 17:04

aryaxt


People also ask

What is shadow opacity?

shadowOpacity : the opacity of the layer's shadow. The value of this property must be in the range 0.0 (invisible) to 1.0 (opaque). The default value of this property is 0.0 .

How does shadow offset work?

ShadowOffset does what its name suggests, it offsets the shadow based on the input. For example, if you have a shadow radius of 10 and have an offset of (5, 5), the shadow will have a width of 5 on top/left and 15 on bottom/right.

What is shadow offset iOS?

shadowOffset controls how far the shadow is moved away from its view. This defaults to 3 points up from the view. shadowOpacity controls how transparent the shadow is. This defaults to 0, meaning “invisible”. shadowPath controls the shape of the shadow.

What is shadowRadius?

The blur radius (in points) used to render the layer's shadow.


2 Answers

self.view.layer.shouldRasterize = YES; self.view.layer.rasterizationScale = UIScreen.mainScreen.scale; 

I was recently having some issues with slow CALayer shadows, and that simple line of code fixed up everything for me!

like image 120
williamcotton Avatar answered Oct 03 '22 04:10

williamcotton


You should expect a slowdown from adding a shadow. A shadowRadius of 20 is very high and will be especially slow.

The other key to improve shadow rendering speed: set the shadowPath property. It can help dramatically.

like image 37
Kurt Revis Avatar answered Oct 03 '22 04:10

Kurt Revis