Image:CocoaNav icon.png

PARMANOIR

Core Animation Bindings

In Core Animation, CALayers 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 CALayers to display data, we need to match source objects and CALayers.

Image:Core Animation Bindings.jpg

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 a NSMutableDictionary to store the CALayers, using their source object's pointer as a key. To know when to create CALayers, loop through the objects array and query the NSMutableDictionary for an existing CALayer. If not found, create a new one. To know when to delete CALayers, loop through the NSMutableDictionary 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 which CALayer to update. (if it's a CATextLayer, update it withlayer.string = [observedObject name], if it's a color change, with layer.backgroundColor = …, etc.)

Sample code

Image:iconZip.png Core Animation Bindings.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