Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - Animation's performance is very poor when view's shadow is on

I have a UILabel with CALayer shadow on.And I just move it around via UIView animation.

The performance is poor and I can see the animation is not smooth at all.

I think it is the shadow of the UILabel which causes the animation problem because if I turn the shadow off, the animation becomes as smooth as normal.

I have tried using view.layer.shouldRasterize = YES;

But still the animation performance is there.

Anyone can give me some hints?

Thanks

like image 407
Jackson Tale Avatar asked Oct 12 '11 21:10

Jackson Tale


1 Answers

You can greatly improve the performance of a CALayer’s shadow by using its shadowPath property—this allows it to draw the shadow without having to recalculate the alpha mask of the layer. For a rectangular view, you’d use it like this:

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

or, if its corners are rounded,

theView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:theView.bounds cornerRadius:theView.layer.cornerRadius].CGPath;

Note that this is a shadow around the view’s borders—if you want better performance on the shadow on the text itself, you either need to use the label’s text-shadow properties (which sacrifice the niceties of CALayer shadows, like blur, for better rendering speed) or—a much more complicated option—create a CGPathRef to use as the layer’s shadowPath from the text glyphs themselves.

like image 142
Noah Witherspoon Avatar answered Sep 22 '22 17:09

Noah Witherspoon