views:

1326

answers:

3

I'm building up a grid of images, and I start at the top, creating UIImageView objects all over the place. Now I want that they overlap a little bit, but the last ones I draw are the ones most on top. I'd like to set the z-position programmatically, so that the first ones I draw will be the ones most on top.

+1  A: 

Not directly.

One solution would be giving the UIViews a z-index property; an integer defining your depth level. Then you could itterate through an NSArray (myArray) containing the UIViews adding them as subviews in order of depth.

Kriem
+1  A: 

Actually, as far as I know, there is no z-index position property for that. Drawing in the view uses a simple drawing model.

You can either put your logic in the drawing loop (i.e draw the things you want on top last) or you can use Cocoa Animation, since unlike UIImageView, CALayers do have depth (they are a 2D projection of a 3D space). See here for a reference.

foljs
yeah, I thought every UIImageView is kind of based on a CALayer?
Thanks
Yes, come to think of it, in the iPhone all views are layer backed.Digging a little, I found the following methods in UIImageView docs:– insertSubview:aboveSubview: – insertSubview:belowSubview:
foljs
+6  A: 

You said you used UIImageView. Drawing an image with UIImageView means initializing it and then adding it to a container view.

Can't you just use this code in your loop?

[containerView insertSubview:image atIndex:[[containerView subviews] count]]

The result would be that the view added first is on the top of the view stack (I haven't been able to test this yet though :) )

ullmark
what would happen if all have the same index?
Thanks
Well, they can't since adding one att index 0 for an example, bumps down all the others in the stack.
ullmark