views:

290

answers:

2

I am wondering what makes Layers different from Views, when every View comes along with it's own Layer. Maybe I am wrong with that. But then: What are these Layers good for?

Like I understand it, I would use an UIView to group elements in my GUI. I could also use that UIView to position things over other things. So where come Layers into place here?

Should I think of them just as the z-index like in HTML / CSS? Are they just interesting for Core Animation stuff?

+2  A: 

Compared to UIVIews, CALayers are

  • lightweight
  • filled with timing information for animation
  • Some things are harder to draw, like text (esp. unicode).

If you have less than 20 or so of them, it probably doesn't matter. If you have complicated animation needs, or just a ton of layers to draw, you may benefit from using CALayers instead.

Kailoa Kadano
In regards to difficult text drawing, you may be interested in the CPTextLayer class within the open source Core Plot framework: http://code.google.com/p/core-plot/ . It does Unicode text drawing and layout within a CALayer.
Brad Larson
A: 

On the iPhone, there's not a significant performance difference between using UIViews and CALayers, in my experience. The Mac is a different story, with NSViews being significantly more heavyweight.

Dealing with CALayers (or at least a view's layer backing) is sometimes necessary for complex animations, particularly keyframe animations or movement along paths.

Additionally, CALayers can be used to create platform-independent (Mac and iPhone) custom user interfaces. UIViews are very different from NSViews, but CALayers are identical between the iPhone and Mac OS X Leopard (aside from a few properties that are missing on the iPhone). I've used CALayers to share UI code between an iPhone application and its Mac counterpart.

Brad Larson