views:

491

answers:

2

Louis - Thanks for your input. Here is what I did, and my current issues, which i don't understand.

The menus (2) are UIImageViews that respond to touch. Each is it's own class. The 'sprites' as you called them also respond to touch.

In the .m where I add the menus and sprites to the layer, I created two container layers, via this code: menuContainer = [CALayer layer].

I ordered them with the menuContainer above the spriteContainer. Now, the sprites do move below the menus - great! But in the process, the sprites stopped responding to touch (I can understand this as they are below the menuContainer layer), and the menus stopped responding to touch as well (don't get that). Continuing to confuse the situation, a layer added to the menuContainer that responds to a multitouch gesture by popping up a slider still reads the multitouch and pops up the slider, but I can't slide the slider. This has me thoroughly confounded.

Thanks.

My app is building a background layer, then building some menu layers that should always be at the top. After this, I add many moving layers and remove many moving layers very quickly.

My problem is I want my menu layers (there are 4) to always be in front of the other layers. I have thought of building a layer hiearchy and using the insertsublayer: atindex: method, making my menus a notional index of 1000, and inserting the multitude of moving layers at a lower index, like 200. If I insert one layer at 200 and then the next at 200, does the first layer that was assigned to 200 get shifted (to 201) or does it get blown away?

I have tried inserting the layers with the insertsublayer: below:, but that does not give me the desired results.

Thanks for the help.

+2  A: 

You can't really do that, the layer index is not a z-order, it is an array index. From the documentation:

This value must not be greater than the count of elements in the sublayer array.

I think you would be best served but actually making a true hierarchy of layers as opposed to trying to shove all of you active layers into one super layer. In other, but all of your menulayers into a container layer, then insert that in the root layer where you want it. Likewise, insert all your sprites into one container layer, and put that in the root layer where you want it.

Additional stuff based on your edits:

Layers are a essentially a graphical construct, they do not directly respond to events. You either need to handle that yourself by writing code to hitTest them in the view they are attached to, or you need to use UIViews instead of layers. You were probably getting away with it before because you were manipulating the layers in such a way that the view hierarchy and layer hierarchy were consistent, but it was not clear to me from your original question that you were using views and not a purely layer based setup.

Louis Gerbarg
Louis, I thought the same thing from that comment in the documentation, but according to the layer geometry and transforms page on the doc site: "The zPosition property specifies the z-axis component of the layer's position. The zPosition is intended to be used to set the visual position of the layer relative to its sibling layers. It should not be used to specify the order of layer siblings, instead reorder the layer in the sublayer array." which to me suggests the layer index *is* z-order. I've written a little code to confirm this and it appears to be the case.
Matt Long
I was not entirely clear I suppose. The index absolutely determines what displays on top of what, so in some sense it is a z-ordering. But it is not a generic z-order in the sense that you can't set them to arbitrary values and expect them to order consistently based on that, the indexes of the various layers will change as you insert other layers.
Louis Gerbarg
A: 

Louis - Thanks for your input. Here is what I did, and my current issues, which i don't understand.

The menus (2) are UIImageViews that respond to touch. Each is it's own class. The 'sprites' as you called them also respond to touch.

In the .m where I add the menus and sprites to the layer, I created two container layers, via this code: menuContainer = [CALayer layer].

I ordered them with the menuContainer above the spriteContainer. Now, the sprites do move below the menus - great! But in the process, the sprites stopped responding to touch (I can understand this as they are below the menuContainer layer), and the menus stopped responding to touch as well (don't get that). Continuing to confuse the situation, a layer added to the menuContainer that responds to a multitouch gesture by popping up a slider still reads the multitouch and pops up the slider, but I can't slide the slider. This has me thoroughly confounded.

Thanks.

KLC
This isn't really an answer; it should be an edit to your question.
Sixten Otto