Image:CocoaNav icon.png

PARMANOIR

Core Animation Phantom Fade

Have you ever seen ghosting when creating layers and animating them to a new position ? I have, and I've been puzzled. In CocoaNav, the frameworks gradually move to the back, and once they're loaded they all move up front on the same plane. Once I messed up so bad to actually see Core Animation fading between the two positions !

Animating squares, this looks like that :

Image:Core Animation Phantom Fade.png

You're expecting layers to smoothly move to their new positions, but they're fading instead. Turns out it's because creation and animation are done in the same transaction. You're telling CA to create and animate, CA then tries to move from N objects to N+1 objects smoothly. It does that with a fade.

The fix

Do it in two transactions. Use a zero duration transaction for creating your layers, then animate them.

// Creation transaction, zero duration
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:0] forKey:kCATransactionAnimationDuration];
[self addRect:NSMakeRect(0, 50, 90, 100)];
[CATransaction commit];

// Animation transaction, whatever duration you want
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
[self animateLayers];
[CATransaction commit];

You'll then see what you expected : smooth animation to the new positions.

Sample Code

Image:iconZip.png CoreAnimationPhantomFade.zip

Core Animation

2008 07 25Where in build phases is that file ?
2008 07 192Imagine clickable error messages
2008 07 18Succulent Stormhoek
2008 07 173Loving the for
2008 07 12(Parmanoir) Feed now validates
2008 07 108Telling classes from instances
2008 07 08Comma Trick
2008 07 06Using libffi
2008 07 04BridgeSupport's type and type64
2008 07 042Clickable Disabled MenuItems
2008 07 026Less bugs through compiler optimizations
2008 06 251CocoaNav JS, a light CocoaNav for Safari
2008 06 232NSWindow goodies : bottomCornerRounded, usesLightBottomGradient
2008 06 222Inspecting NSUndoManager's undo stack
2008 06 16Cocoa Regular Expressions via JavascriptCore
2008 06 15Crossing the WebKit bridge
2008 06 08Double and Triple Click
2008 06 05Photoshop-like compositing with Core Animation
2008 06 052One way binding to NSSlider
2008 05 30Threaded Core Animation, Part Deux
Image:rss.png
Image:rss.png

Powered by MediaWiki

Hi ! I'm learning Cocoa to (hopefully !) become an indie developer.

I've written software all my professional life, in C++, PHP, Javascript. I've designed websites and web interfaces. My last venture went into flames as clients were happy but didn't like paying very much.

I've had little luck in the B2B world, I'm hoping for a better future writing Mac applications.

Planet Cocoa