Question

I have several UIImages and I want to the user to be able to drag each one individually. I want them to be able to touch an image (must touch image not somewhere else), and drag it across the screen without effecting the other images. This code moves both images at the same time to the same place, no matter where the user touches on the screen:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
playerCardOne.center = location;
playerCardTwo.center = location;
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}

I tried using an if statement like this, and it stops the action completely, no dragging at all:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (touch.view == playerCardOne) {
    playerCardOne.center = location;
    [self.view bringSubviewToFront:(playerCardOne)];
}
else if ([touch view] ==playerCardTwo) {
playerCardTwo.center = location;
[self.view bringSubviewToFront:(playerCardTwo)];
}
}

Can anyone help?

Was it helpful?

Solution

Try putting this in your

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
   UITouch *touch = [[event allTouches] anyObject];
   CGPoint location = [touch locationInView: self.view];

   if ([touch view] == playerCardOne) 
   {
       playerCardOne.center = location;
   }
   else if ([touch view] == playerCardTwo) 
   {
       playerCardTwo.center = location;
   }
}

and take out:

[self touchesBegan:touches withEvent:event];

And make sure to set userInteractionEnabled = YES;

OTHER TIPS

You can use below. I used this for multiple images moving on screen. It's working for me.

UIPanGestureRecognizer *span=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan:)];

 [smyImageView addGestureRecognizer:span];
smyImageView.userInteractionEnabled = YES;


   UIPanGestureRecognizer *span1=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan1:)];

   [smyImageView1 addGestureRecognizer:span1];

Moving (Pan):

- (void)onsPan:(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];

}

- (void)onsPan1:(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];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top