Re: Understanding layer-backed views
Re: Understanding layer-backed views
- Subject: Re: Understanding layer-backed views
- From: Graham Cox <email@hidden>
- Date: Wed, 22 Jun 2011 14:33:01 +1000
On 22/06/2011, at 2:21 PM, Scott Anguish wrote:
>> I'm getting confused and frustrated by Core Animation layers (again). Mac OS.
>>
>>
>> If I simply add a NSView to a window in IB, not subclassed, but turn on Core Animation layer for that view and set a background colour, shouldn't it appear with that colour in the window? That's my expectation, but that's not what I get. Instead, I see nothing. I can log the fact that the layer exists, and it has apparently the right dimensions and so on, but nothing is actually displayed.
>
> Setting a background color to the layer?
>
> The order of creation of the layer is the entire dependency here. This is discussed in the animation overview.
>
> There are to types of layer/view interactivity
>
> Layer-backed - the view uses the layer as a cache of sorts. You should leave the layers alone. Period. You just call setWantsLayer: and the view makes a layer. If you replace it, it still isn’t your’s to touch. (and you probably shouldn’t)
>
> Layer-hosted - the view hosts a layer-tree that you create. The difference is that you create a layer, do setLayer: on the view, and then turn on setWantsLayer:. If you don’t do it in that order, you should not touch the layers.
>
> it’s entirely the order of setLayer: setWantsLayer: that controls whether you should touch the layers. If you do it this order, you can, if you do setWantsLayer: first, hands off.
Yep, I'm aware of the setWantsLayer/setLayer order making a difference.
In my case I want a layer-hosting view, not a layer-backed view.
However, my question isn't really about that, but about what is done when you check the box next to the layer in Interface Builder, in the 'setWantsLayer' panel. Does this provide layer backing or layer hosting? My thoughts were that is provides layer hosting, but I'm not 100% sure. Proceeding on the basis that this is the case, it seems to work.
The next thing was having checked this box, should I see anything? I was thinking that setting a colour under the appearance section should set the background colour. It looks as if in fact that sets the SHADOW colour, though it's not that clear from the layout of the controls in IB, and the fact that it's not disabled when shadow is unchecked. So I just got a bit confused and sidetracked by that. If I set the layer's background colour in code, it shows up. Seems odd that IB provides controls to set some not very interesting layer properties (shadow) and nothing for stuff you use all the time (background, border, corner radius).
> Again this is not in line with my expectations, which is that the frame of a sublayer is expressed in the coordinate system of its superlayer, not the underlying window.
I found the cause of this issue, which was that in my layer delegate, I was not saving, setting and restoring the context passed to drawLayer:inContext:. I was assuming that the context was set, but having found a code snippet in the docs that indicated it wasn't, I added these calls and my graphics are drawing in the right place now.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden