Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-19-2012, 07:25 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Question Jerky animation with shadows?

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:

Quote:
myImageView.layer.masksToBounds = NO;
myImageView.layer.shadowColor = [UIColor whiteColor].CGColor;
myImageView.layer.shadowOffset = CGSizeMake(0,2);
myImageView.layer.shadowRadius = 2;
myImageView.layer.shadowOpacity = 0.2;
Is this normal behavior?

How can I avoid jerky animation when using shadows?
samurle is offline   Reply With Quote
Old 01-19-2012, 08:21 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by samurle View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-19-2012, 08:42 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Question

Quote:
Originally Posted by Duncan C View Post
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.

Last edited by samurle; 01-19-2012 at 08:46 PM.
samurle is offline   Reply With Quote
Old 01-19-2012, 08:47 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by samurle View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-19-2012, 10:59 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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.
samurle is offline   Reply With Quote
Old 01-20-2012, 02:36 AM   #6 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

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.
sneaky is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, jerky, layer, shadows

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 407
11 members and 396 guests
7twenty7, Atatator, FrankWeller, glenn_sayers, guusleijsten, iphonedevshani, MAMN84, QuantumDoja, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:45 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0