开发者

iPhone layer management - best practice

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 a开发者_JAVA百科t 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.


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 - 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜