I am implementing a simple iOS solitaire game that allows the user to drag the cards around in the usual way. The cards are represented with the UIView subclass CardView. All the card view’s are siblings which are subviews of SolitaireView. The following snippet tries to “bring a card to the front” so that it is above all the other views as it is being dragged:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view.tag == CARD_TAG) {
CardView *cardView = (CardView*) touch.view;
...
[self bringSubviewToFront:cardView];
...
}
}
Unfortunately, the card’s z-order remains unchanged during the drag. In the images below, I am dragging the King. Notice how it is correctly on top the Nine in the left image, but is incorrectly under the Two (under the entire stack actually) in the right image:

I also tried alter the layer.zPosition property as well to no avail.
How can I bring the card view to the front during the drag? I am mystified.
Confirmed.
bringSubviewToFront:causeslayoutSubviewto be invoked. Since my version oflayoutSubviewssets the z-orders on all the views, this was undoing the z-order I was setting in thetouchesBegan:withEventcode above. Apple should mention this side effect in thebringSubviewToFrontdocumentation.Instead of using a
UIViewsubclass, I created aCALayersubclass namedCardLayer. I handle the touch in myKlondikeViewsubclass as listed below. topZPosition is an instance var that tracks the highest zPosition of all cards. Note that modifying thezPositionis usually animated — I turn this off in the code below: