views:

822

answers:

3

I'm slightly confused when to use CALayer on the iPhone or Mac and when not to use it? CoreAnimation works just fine on my UIView based objects without having to use CALayer. When is the appropriate time to dig into this class?

+3  A: 

If you can do what you want with 'implicit animation' (that offered by UIKit/AppKit without having to dig into CA,layers, and animators) then definitely go that route.

CoreAnimation comes into play when you start using more complex animations, such as non-linear motion, or repeating effects, and certain synchronized effects. There's a LOT you can do with it, but it is a pretty heavy duty tool (with a commensurate learning curve, at least compared to the UIKit stuff).

Ben Gottlieb
I think that this statement is misleading. The level of abstraction that Core Animation provides is remarkable. The API is very easy to pick up and work with. I understand that you are comparing it to UIKit but calling it 'heavy duty' and using the words 'learning cure' is discouraging and I think misleading. The Core Animation Programming Guide could use a little work though for sure.
Joe Ricioppo
I agree with Joe; UIViews are just a thin wrapper on top of CALayer and UIResponder. If not for Interface Builder, CALayers wouldn't be much more difficult to work with than UIViews (and for anyone who develops on OpenGL, both are greatly simplified)
rpetrich
okay, perhaps I was a little harsh in my description: CoreAnimation IS awesome, and easy once you get the hang of it. However, it's an order of magnitude more work than simply wrapping a setFrame: message in begin/commit animation calls.
Ben Gottlieb
+7  A: 

In my benchmarks, UIView and CALayer provide about the same level of performance on the iPhone. As rpetrich mentions in his comment, UIViews are a thin wrapper around CALayers. On the Mac, CALayers are much more lightweight than NSViews.

As Ben points out, you can go beyond the capabilities of implicit animations by working directly with CALayers, even providing some 3-D effects through CATransform3D. In many cases, you can do this even with your standard views by accessing the backing layer (if the view is layer-backed).

Another concern is cross-platform (Mac / iPhone) code. My iPhone application uses an all-CALayer interface for its primary view in large part because I can use the exact same code for drawing that interface in its Mac counterpart. For another example of this, I direct you to the Core Plot framework, which draws graphs entirely using CALayers and works on both Mac and iPhone. CALayers are pretty much the same on both platforms, where UIView and NSView have very different interfaces.

Brad Larson
A: 

The short and simple answer (as of autumn 2010),

(*) on iPhone everything is just UIViews, don't worry about layers explicitly (Exception mentioned below)

conversely,

(*) on MAC, NSViews are junk. They should be retired. The z-ordering never works, etc etc etc. In practice, on the Mac other than the simplest things, just go directly to CALayers to save yourself the pain of wasting time with NSViews before abandoning NSViews and going to CALayers.

(Cocoa / Mac technology is looking very old-fashioned if you're an iFone programmer - it's lame!)

That's the bottom line.

(*) Exception -- Brad makes a spectacular point: if you go to the effort of doing iFone using Layers, you can magnificently port to MAC based on that (if, for some reason, you have to port from Fone to MAC). I would say that other than the simplest apps if you are porting from Fone to Mac, you will have to use CALayers on the Mac and that's that.

Again for Mac don't waste time -- make the leap to CALayers, you'll be glad you did.

Joe Blow