I did some reading on Core Animation over the weekend and one thing kept coming up. Layers, the hosting or backing of layers in NSView classes. Mind you, most of my reading dealt with Core Animation as it applies to MacOS not iPhone. You should know there are major differences which is annoying to an AS/400 guy coming from an ASP/COM+ background to the Java platform attempting to pick up CocoaTouch by way of Mac Desktop programming textbooks. Most Mac devs will tell you, “…go pick up the Aaron Hillegass book for Mac if you want to learn iPhone stuff. It’s pretty much standard for both platforms, except where it’s different.” I’m also realizing that most hyperlinks that come up for “core animation” specific Google searches are going to assume you want to create Leopard apps while the text I’ve seen for Core Animation and CALayers on iPhone only explains that the APIs exist. But that’s not what I wanted to explain to you today. Hi, I’m Cliff. You’re here because you don’t know the difference between layer backing and layer hosting views. I’m here because I keep forgetting to pay attention to the “NS” and “UI” prefixing the word “View” in the APIs I’m trying to learn.
Today I’m just going to post a little tidbit that I think I learned about CoreAnimation. Say you’re writing software for the Mac Desktop, right? No, really. Say it out loud because you need to admit it to yourself. Just get over the fact that you’re never going to learn iPhone programming in a weekend and start accepting the fact that the desktop APIs are the only ones that get the coverage and attention they deserve. Now that you’ve said all of that, let’s pretend that you want to do some lower level animation stuff involving CALayers. You’re going to come across documentation that differentiates between layer backing and layer hosting. That documentation isn’t going to explain the difference it will just warn you of the difference. That’s why you’re here. That’s why a lot of people come here. You want to know what the difference is.
Layer Backing (and my disclaimer states I might have these terms confused.)
When an NSView (not a UIView, ok?) is said to be a layer backing view it means that a separate area of memory, or cache if you will, is set up for the on screen display. That is the view is backed up by the CALayer APIs allowing more advanced animation and all. It does NOT mean that you can do things (like modify and manipulate) the layers backing up the view because the UIView, *ahem* NSView… my apologies, owns those layers. Simply stated Layer backed views own their layers and are licensed to do whatever nonsense they wish to do with those layers. Such nonsense includes but is not limited to, the deallocation of a current set of layers which include the several hundred hours you spent setting them up, the recreation of new layers in the format/shape that the view deems appropriate, and more.
Layer Hosting Views
When an NSView, I mean UIView… sorry that’s a Mac-Desktop-thingy-View (what do they begin with again? UI or NS?) When one of those views is given a layer programatically it is said to be a layer hosting view. That means it does not interfere, touch, or sneeze on the layers it is hosting. You, the developer, are responsible for everything in/on/near those layers which is a vewy, vewy, big job to do. With a layer hosted view you set the root layer in the view via the view’s layer property. That layer and every sublayer from then on is your to do your evil bidding. You may ad layers, subtract layers, and sprinkle layers with chocolate without fear of your hard work being trashed by a framework that feels it knows more than you.
The difference is in code. It boils down to (don’t you just hate when people always say something boils down to something else? Who boils their ObjC code anywayz?) when you invoke the [view setWantsLayer:(BOOL)layerReqd] method. The moment you call this method the view will decide if it needs to create and manage its layers. It it already has a layer (given by you the developer) then it clocks out for the day leaving the remainder of the management to you. If its layer has not been set then it will create a CALayer and begin managing it internally. To put it plainly:
[oneView setLayer:myProgramaticLayer]; //you own this layer [oneView setWantsLayer:YES]; //oneView is layer HOSTING [anotherView setWantsLayer:YES]; //anotherView is layer BACKED //Aha! you don't own this view! It was created by the layer. CALayer *anotherViewsLayer = [anotherView layer];
That just about summarizes it. Check back next week when I discuss iPhone audio filters and running 3rd party codecs on device (if I can ever get that working). I’ll holler at’cha…
The act of “hollering” is not intended for derogatory inclination nor is it meant as a means of dehumanizing the reader. Hollering in its raw form refers to the notion of an informal greeting as it is shared between two friends, chums, co-workers, or people passing by in the street. As such you, the reader are invited to “holler back” as you deem appropriate by utilizing the squared space below. All comments, replies, shout-outs, and/or hollering recorded by the square becomes property of Craig Conglomerates. In clicking the submit button, you hereby grant permission to the author to re-state, mis-interpret, and claim credit for any responses provided. Some restrictions may apply in certain states and/or jurisdictions. See your local legal official for details.