Last Updated: February 25, 2016
·
2.804K
· alistairholt

Improve the performance of UIView animations when using shadows on your UIViews

If you're using UIView animations with shadows on your UIViews then you are going to find your animations jumpy and slow. If your views are rectangular - including rectangles with rounded corners - then you can speed things up by using CALayer's shadowPath property.

For plain old rectangles:

yourView.layer.shadowPath = [UIBezierPath bezierPathWithRect:yourView.bounds].CGPath;

For rectangles with rounded corners:

yourView.layer.shadowPath = [UIBezierPath bezierPathWithRect:yourView.bounds cornerRadius:yourview.layer.cornerRadius].CGPath;

This allows you to bypass Core Animation's expensive off-screen rendering to figure out the shape of the view before it can render the drop shadow.

Important: If the bounds of your view change, don't forget to update the shadowPath to reflect the changes! If you're going to animate any changes to the bounds then you'll need to go a step further and look into CAAnimations to animate the shadowPath CGPath.

1 Response
Add your response

Thanks a lot! This improved the performance of my game greatly!

Awesome!

over 1 year ago ·