2008 04 24Core Animation Bindings
In Core Animation,
CALayer
s are live : they're created and stay here until they're destroyed, just like DOM nodes in an html page. This is in contrast to Cocoa drawing, or the canvas
html tag, where drawing is done programmatically when needed. So when using CALayer
s to display data, we need to match source objects and CALayer
s.
Just a hash
We use a custom view CAListView
and observe data in an NSArrayController
. To update the view, we need to know when to create / delete layers and when to update them to reflect their source object.
- creating/deleting layers observe changes by binding to
arrayController.arrangedObjects
— this will tell us when objects are created, moved, and deleted. Use aNSMutableDictionary
to store theCALayer
s, using their source object's pointer as a key. To know when to createCALayer
s, loop through the objects array and query theNSMutableDictionary
for an existingCALayer
. If not found, create a new one. To know when to deleteCALayer
s, loop through theNSMutableDictionary
and check if its keys (pointers to objects) are still in the objects array. If not, delete them. - updating layers bind once more with bastard observing to observe all the keys of all the controller's objects. When an object changes, query the
NSMutableDictionary
for knowing whichCALayer
to update. (if it's aCATextLayer
, update it withlayer.string = [observedObject name]
, if it's a color change, withlayer.backgroundColor = …
, etc.)
Sample code
Core Animation
- CocoaNav a Cocoa Class Browser using Core Animation
- Core Animation Starfield Core Animation sample using 3D layers
CoreAnimationStarfield.zip - Core Animation culling problems Don't setup a projection transform on the root layer !
- Threaded Core Animation while on the main thread, update your display with
[CATransaction commit]
CoreAnimationUpdateOnMainThread.zip - Core Animation Bindings binding Cocoa objects to
CALayer
s
Core Animation Bindings.zip - Core Animation Phantom Fade seeing ghosting ? Use two transaction to create and animate your objects
CoreAnimationPhantomFade.zip - Photoshop-like compositing with Core Animation
Blending Modes.zip