I'm making my first app that is much like a solitaire game - the user has to move cards around and stack them onto each other. Now I'm very new to Objective-C so I hope you forgive me when my question is stupid.
So I'm trying things out and I have two UIImageViews
with UIPanGestureRecgonizer
attached to them. So whenever a user drags one of the cards it must be on top of the other card. I'm using the code below in my ViewController.m file but the problem is that when I drag one card and then want to drag the other card, the first card snaps back to its original position, but I would like it to stay where I dragged it. When I comment out the bringSubviewToFront then this does not happen, but one of the cards always stays behind the other.
-(IBAction)dragCard:(UIPanGestureRecognizer *)recognizer {
UIView *cardView = [(UIPanGestureRecognizer *)recognizer view];
[self.view bringSubviewToFront:cardView];
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];}
I cannot figure out what's causing this. Can you please help me...
Edit: I logged the self.view.subviews to show what's happening. I have dragged the image with tag 1 and now I start dragging image with tag 2. You can see that both coordinates change - image with tag 1 moves back to original position and image with tag 2 also moves:
2013-05-16 17:02:20.424 (
"<UIImageView: 0x71749e0; frame = (0 0; 320 411); autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7174a40>>",
"<UIImageView: 0x7172ef0; frame = (25 77; 42 58); autoresize = TM+BM; tag = 1; gestureRecognizers = <NSArray: 0x7171700>; layer = <CALayer: 0x7171620>>",
"<UIImageView: 0x71722f0; frame = (88 200; 42 58); autoresize = TM+BM; tag = 2; gestureRecognizers = <NSArray: 0x7171bc0>; layer = <CALayer: 0x7172350>>"
2013-05-16 17:02:20.441 (
"<UIImageView: 0x71749e0; frame = (0 0; 320 411); autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7174a40>>",
"<UIImageView: 0x7172ef0; frame = (30 177; 42 58); autoresize = TM+BM; tag = 1; gestureRecognizers = <NSArray: 0x7171700>; layer = <CALayer: 0x7171620>>",
"<UIImageView: 0x71722f0; frame = (91 201; 42 58); autoresize = TM+BM; tag = 2; gestureRecognizers = <NSArray: 0x7171bc0>; layer = <CALayer: 0x7172350>>"
The first ImageView is my background image.