Change the z-order of subheadings on iPhone

I am developing a game. I am using about 150 UIImageView to store graphics. I am simulating a 3D environment, so I would like to change the z-order (how close the object is to the camera).

I know it exists: [superWindow exchangeSubviewAtIndex: I withSubviewAtIndex: j];

But for some reason it doesn't work, some of the sub-items disappear again.

Now I just delete all subviews and add it again in the correct z order, that's ok with 50 subviews (on a 2G iphone), but from 120 it takes half a second, so the gameplay sucks (I don't have 3GS either so I didn't try it there).

I use so many subplots because each one is a square, then I colored it, resized it and moved it somewhere on the screen. I maintain subviews under NSMutableArray ...

+2


source to share


5 answers


The iPhone documentation often warns that multiple UIViews are nested, as after a certain point it has a lot of success. If you're starting to get into trouble, one option is to map your UIViews to an image and use that to reduce the number of screen views, but if you are simulating 3D that probably won't help as your composite view needs to be re-rendered too often.



The iPhone has full OpenGL ES support to create a real 3D environment. Take a look at some samples and / or a good book on this topic and you will find that it is much easier to just use OpenGL.

+5


source


Z-order is the artifact in which your view appears in your list of subviews supervisors. The last subview added to the supervisor is "on top".

So, to move a view to the "top", you remove it from your supervisor, then add it again, making it last and therefore on top.

Here the view moves to the top of the z-order.



// move to the to in the z-order
UIView* superview = [self superview];
[self retain];
[self removeFromSuperview];
[superview addSubview:self];
[self release];

      

Pay attention to [self save] to prevent your dealloc if your supervisor is the only reference to you.

+4


source


If you don't want to dive into OpenGL ES as Timothy suggests, you can look at replacing UIViews with CALayers and put them in a CATransformLayer . CATransformLayer is new in iPhone OS 3.0, and it allows you to perform 3D CALayers layouts. Then you can set the zPosition property on your CALayers to find them in the Z-plane of the CATransformLayer. For more on 3D layout CALayers, I guide you in this article .

You can even add a nice perspective effect to the overall CATransformLayer by adjusting the m34 element in the CATransform3D application applied to that layer.

Moving from UIViews to CALayers is pretty straightforward because their structures have a lot in common.

+2


source


Consider using a 2D game library like Cocos2D . I'm pretty sure it supports Z-ordering and the performance will be much better than using UIViews.

+1


source


I know this is old, but you can change the z-order with bringSubViewToFront.

Check out this answer: How to set UI index for iPhone i?

+1


source







All Articles