Reputation: 14418
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?
Upvotes: 5
Views: 2057
Reputation: 29508
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.
Upvotes: 16
Reputation: 33423
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.
Upvotes: 1