4

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.

flouwer
  • 327
  • 1
  • 4
  • 13
  • Why every time you are creating new object of cardView.Please declare it into .h file ans use the same and tell me what happens – iEinstein May 15 '13 at 11:49
  • Thanks for the suggestion, but the problem still exists – flouwer May 15 '13 at 12:04
  • Are you overriding `layoutSubviews` in your view ? – danypata May 15 '13 at 12:50
  • No I'm not overriding layoutSubviews – flouwer May 15 '13 at 13:01
  • possible duplicate of [iOS >> Dragged View is Jumping Back to Original Position >> Auto Layout Combined with UIPanGestureRecognizer Issue](http://stackoverflow.com/questions/16129988/ios-dragged-view-is-jumping-back-to-original-position-auto-layout-combined) – dontangg Aug 23 '15 at 13:25

1 Answers1

1

Alright, found the answer here. It was an issue with bringSubviewToFront and Autolayout. Sorry about repeating the question.

Community
  • 1
  • 1
flouwer
  • 327
  • 1
  • 4
  • 13