15

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

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

2 Answers2

40

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.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • Yeah it worked. theView.layer.shadowPath = [UIBezierPath bezierPathWithRect:theView.bounds].CGPath; improves my performance very much. It is quite smooth now. Thanks – Jackson Tale Oct 12 '11 at 22:01
  • Where is this text shadow property? – jjxtra Jun 19 '12 at 21:34
  • @PsychoDad: It’s actually two properties, `shadowColor` and `shadowOffset`. – Noah Witherspoon Jun 19 '12 at 22:04
  • Using a shadow path means Core Animation doesn’t have to render out your layer’s opacity mask to have something to blur into a shadow; it can just draw the shadow using the known shape. I don’t know the implementation details, but it’s dramatically faster to do the latter. – Noah Witherspoon Oct 24 '14 at 02:26
0

Not sure if this is the answer you're looking for but I found this: Drop Shadow on UITextField text

It may be better performance, I haven't tried it but it seems it would be.

Community
  • 1
  • 1
mkral
  • 4,065
  • 4
  • 28
  • 53