Imagine a pink square, partially overlapping a green one. The user drags the pink square. If they drag it far enough that it is no longer overlapping the green square, the green square moves to the top of the stack. If they drag the pink square back again, it will now be under the green one.
Here's the code.
-(void)dragView:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
if (!CGRectIntersectsRect(backView.frame, frontView.frame) && !self.performedFlip) {
UIView *backView = recognizer.view.tag == 1 ? self.greenView : self.pinkView;
[self.view bringSubviewToFront:backView];
self.performedFlip = YES;
}
}
But it doesn't work as expected. As soon as the green view is brought to the front, the pink view (that's still being dragged) jumps back to its starting position. It's as if changing the subview hierarchy disrupts the gesture recognizer.
I've done a huge amount of fiddling and logging to try and work out what's happening. To test my theory about the gesture disruption, I tried something different: instead of moving the green view to the top, I added a new subview instead. Same result. Adding a new subview made the object being dragged jump back to its starting position.
Any clues?