Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to do shadows on iOS?

QuartzCore .layer.shadow's suck up performance. They appear to need to be re-rendered every time something changes, causing everything to lag.

Coregraphics gradient (for 1 way shadows) - doesn't look right. if your gradient goes from 0.3 alpha to 0, it has some odd effect where you can 'see' it stop. It just doesn't look nice, or natural. Maybe it isn't dithered, but I'm sure I heard core graphics gradients are. It's odd, I don't know.

Coregraphics shadow - take a while to render as you set them, but otherwise great performance. It's just that second you're waiting for a view to appear because it has to render it's shadow first, that's the problem.

So I must be missing something. Is there another method which looks right, and is speedy both in rendering time and in performance?

like image 960
Andrew Avatar asked Apr 12 '12 23:04

Andrew


People also ask

What is the purpose of IOS Shadow?

The layer's shadow uses several elements: shadowOffset , shadowColor , shadowOpacity , and shadowRadius . Each element changes the respective appearance. You can offset the shadow differently to change the direction the shadow is cast - how far from the layer and in which direction.


1 Answers

Adding a shadowPath should give you a huge performance boost. The following example assumes you only want the shadow on the sides of your view

CGPathRef path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath; [view.layer setShadowPath:path]; 

EDIT: On default a CALayer draws a shadow during animations, the following code allows you to cache the shadow as a bitmap and reuse it instead of redrawing it:

self.view.layer.shouldRasterize = YES; // Don't forget the rasterization scale // I spent days trying to figure out why retina display assets weren't working as expected self.view.layer.rasterizationScale = [UIScreen mainScreen].scale; 
like image 90
aryaxt Avatar answered Sep 16 '22 16:09

aryaxt