I'm using layer shadows, and I've noticed when testing my app on a real device, UIView animation is very jerky.
Only 6-8 UIViews are using shadows, but even if I use just one, animation is still jerky.
Even the alert boxes that pop up are affected by the shadows. It kills their animation.
When testing on the iPhone simulator, animation is smooth. The problem only happens on a real device.
The jerky animation goes away when I comment out the shadows, but I still would like to use them.
I'm using layer shadows, and I've noticed when testing my app on a real device, UIView animation is very jerky.
Only 6-8 UIViews are using shadows, but even if I use just one, animation is still jerky.
Even the alert boxes that pop up are affected by the shadows. It kills their animation.
When testing on the iPhone simulator, animation is smooth. The problem only happens on a real device.
The jerky animation goes away when I comment out the shadows, but I still would like to use them.
Example of what I'm using:
Is this normal behavior?
How can I avoid jerky animation when using shadows?
Sadly, the answer is to not use shadows. They cause a huge performance hit. The problem is that shadows are partly transparent. That means that pixels from the layers underneath have to be blended with pixels on the top layers. It gets worse if the shadows overlap. The graphics hardware on iOS devices is highly optimized for tiled rendering, where each layer is a discrete block that can just be "blitted" to the screen's frame-buffer. When you use shadows, that optimization doesn't work, and the graphics hardware has to composite all those layers manually.
The simulator uses the graphics hardware on your mac, which is tens, perhaps hundreds, of times faster than the graphics hardware on your iOS device. Your mac's graphics card probably has several times more memory than your entire iOS device, just for example.
I created a very cool view controller transition where I cut up the front view controller into a whole bunch of vertical "slats" that I then lift off the page and flip over 180 degrees in a left-to-right cascade. It runs quite smoothly with 20 or 30 slats animating at once, unless I turn on shadows for those layers. When I do that, the animation gets all jerky, stalls and suddenly jumps, and lots of other bad things.
Like you, I found that it runs beautifully with shadows on the simulator.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Sadly, the answer is to not use shadows. They cause a huge performance hit. The problem is that shadows are partly transparent. That means that pixels from the layers underneath have to be blended with pixels on the top layers. It gets worse if the shadows overlap. The graphics hardware on iOS devices is highly optimized for tiled rendering, where each layer is a discrete block that can just be "blitted" to the screen's frame-buffer. When you use shadows, that optimization doesn't work, and the graphics hardware has to composite all those layers manually.
The simulator uses the graphics hardware on your mac, which is tens, perhaps hundreds, of times faster than the graphics hardware on your iOS device. Your mac's graphics card probably has several times more memory than your entire iOS device, just for example.
I created a very cool view controller transition where I cut up the front view controller into a whole bunch of vertical "slats" that I then lift off the page and flip over 180 degrees in a left-to-right cascade. It runs quite smoothly with 20 or 30 slats animating at once, unless I turn on shadows for those layers. When I do that, the animation gets all jerky, stalls and suddenly jumps, and lots of other bad things.
Like you, I found that it runs beautifully with shadows on the simulator.
Thanks, are you sure the problem is just transparency-related, or is it specifically something
to do with UIView shadows?
I use plenty of UIFont text with shadow offsets, and animation is very smooth.
Perhaps it is just related to UIView shadows?
Would creating a pre-rendered shadow image help? I could use the UIGraphics methods to draw into
a UIImage that's been resized a bit larger, so the shadow fits. It would still be transparent,
but not a layer shadow.
Thanks, are you sure the problem is just transparency-related, or is it specifically something
to do with shadows?
Would creating a pre-rendered shadow image help? I could use the UIGraphics methods to draw into
a UIImage that's been resized a bit larger, so the shadow fits. It would still be transparent,
but not a layer shadow.
I'm fairly sure, but not certain. You might want to post a question to the Core Animation thread on Apples iOS developer forum. There are couple of Apple engineers who post there regularly, and really know it well.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
I'm fairly sure, but not certain. You might want to post a question to the Core Animation thread on Apples iOS developer forum. There are couple of Apple engineers who post there regularly, and really know it well.
Thanks, I have a feeling that it will work if layer shadows are rendered into a UIImage, because
I'm already using a huge 320x480 transparent .png as my background image, and animations are smooth.
But, this is tricky for me to do, since layer shadows are rendered outside the UIImageView.
And it's even tricker if I'm using UIViewContentModelScaleAspectFit, since the size changes on me.
What a mess to use shadows.
It does seem like a big performance hit trying to blend a shadow. I can't answer the question as to what exactly is the problem but I have seen it as well.
My solution was to zoom in the view that was about to get animated and then hide the shadow during the animation. zoom out and apply the shadow again.
This had the effect of looking like the view was brought so much closer to to user that it didn't have a shadow anymore, like lifting up and preparing to animate.
This worked for me as I had smaller views that I could do all of this with. If the animation is faster just hide the shadow - animate - show the shadow again.
Would be lovely to be able to have the shadow while animating but that simply doesn't seem to be an option at the moment.