Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding shadow to cell causes laggy scrolling

So I have a UIView which has a shadow:

[containerFrame.layer setShadowOffset:CGSizeMake(0, 1)];
[containerFrame.layer setShadowRadius:4.0];
[containerFrame.layer setShadowColor:[UIColor colorWithRed:34/255.f green:25/255.f blue:25/255.f alpha:1.0].CGColor];
[containerFrame.layer setShadowOpacity:0.4];

with this in place, my scrolling FPS drops to 20-30. Remove the shadow and then boom, my FPS is back to 60 and scrolling is as smooth as butter. Now the question is I need ti have a shadow effect around this box/container view. How do I achieve this without slowing down scrolling?

like image 779
aherlambang Avatar asked Jul 31 '12 04:07

aherlambang


2 Answers

Try setting the shadow path:

[containerFrame.layer setShadowOffset:CGSizeMake(0, 1)];
[containerFrame.layer setShadowRadius:4.0];
[containerFrame.layer setShadowColor:[UIColor colorWithRed:34/255.f green:25/255.f blue:25/255.f alpha:1.0].CGColor];
[containerFrame.layer setShadowOpacity:0.4];

// New line
[containerFrame.layer setShadowPath:[UIBezierPath bezierPathWithRect:containerFrame.bounds].CGPath];

If you have to animate this view (and especially if it’s part of a UITableViewCell) you will probably notice stutters in the animation. This is because calculating the drop shadow for your view requires Core Animation to do an offscreen rendering pass to determine the exact shape of your view in order to figure out how to render its drop shadow. (Remember, your view could be any complex shape, possibly even with holes in it.)

From On the importance of setting shadowPath.

like image 96
Michael Robinson Avatar answered Oct 03 '22 17:10

Michael Robinson


Set containerFrame.layer.shouldRasterize = YES; The reason it slows down is because calculating the shadow is expensive. Rasterizing will collapse the view into an image so it will be much quicker.

like image 41
borrrden Avatar answered Oct 03 '22 19:10

borrrden