One of the up and coming interface styles these days is windows, particularly inspector palettes and the like, that have their individual sections expand and collapse. Call it “accordion” view, which we prefer, or “animated outline” view, or “modular” view, or “collapsing” view, or “disclosing” view, or whatever, it’s a nifty thing to do. But it’s rather complicated to get right. As we know for a fact, from implementing a kinda-sorta version of it for the Vonage Companion SIP client a while back.
Well, there’s help now: over at espresso-served-here you can find what looks like a very slick implementation of the idea, Core Animation cool and everything:
To make one of these views isn’t quite as easy as you first think, and you can quickly go down the wrong road in the design. The super-secret trick is to *not* use Core Animation, or more precisely: ignore the lure of
NSViews conformance to theNSAnimatablePropertyContainerprotocol. As of Mac OS X 10.5, a few of the properties of a view or window can be animated simply be replacing calls like[view setFrame:newFrame];with[[view animator] setFrame:newFrame];. This is fine when you have one or two views that you want to move but it’s impossible to coordinate the movements of multiple view using this API. After using Core Animation, one would expect that after a call such as[[view animator] setFrame:newFrame];requesting the frame from the view would return newFrame. Unfortunatley it returns the original frame of the view. Only when the animation is done, do you get newFrame. Furthermore, just try setting the delegate of theCABasicAnimationobject that handles the implicit animation and getting any form of useful information back. You can’t. You’re in for a world of pain if you try.The solution: use good-ol’
NSViewAnimation. That way you can create all the dictionaries containing the animations for all the views you want to, then instantiate oneNSViewAnimationobject to handle the lot and set them off at the same time. Simple.
Indeed. Although that’s a definition of “Simple.” which is not in terribly common use, wethinks. Much, much easier to just go grab the tlanimatingview project over at Google Code, yes?
