Search code examples
iosios5ios4xcode4.3

iOS How to use touchesMoved to move two images?


I have two different UIImages. I want to allow user to move these images over the UIView individually. How could I do that?

IBOutlet UIImageView *image01;
IBOutlet UIImageView *image02;

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

Solution

  • I'm not sure if I understand the question. You can use the standard pan gesture recognizer, one for each UIImageView, e.g.:

    UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
    image01.userInteractionEnabled = YES;
    [self.image01 addGestureRecognizer:pan1];
    
    UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
    image02.userInteractionEnabled = YES;
    [self.image02 addGestureRecognizer:pan2];
    

    and

    - (void)handlePanImage:(UIPanGestureRecognizer *)sender
    {
        static CGPoint originalCenter;
    
        if (sender.state == UIGestureRecognizerStateBegan)
        {
            originalCenter = sender.view.center;
            sender.view.alpha = 0.8;
            [sender.view.superview bringSubviewToFront:sender.view];
        }
        else if (sender.state == UIGestureRecognizerStateChanged)
        {
            CGPoint translation = [sender translationInView:self.view];
    
            sender.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
        }
        else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
        {
            // do whatever post dragging you want, e.g.
            // snap the piece into place
    
            [UIView animateWithDuration:0.2 animations:^{
                CGPoint center = sender.view.center;
                center.x = round(center.x / 50.0) * 50.0;
                center.y = round(center.y / 50.0) * 50.0;
                sender.view.center = center;
                sender.view.alpha  = 1.0;
            }];
        }
    }
    

    Or you could create a gesture recognizer on their superview and then use CGRectContainsPoint to see if your starting locationInView is within the frame of one of your two UIImageViews, and then proceed to animate the appropriate one.

    For example, in your view controller, you could create a gesture recognizer:

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSuper:)];
    [self.view addGestureRecognizer:pan];
    

    And the handler might look like (you could implement as a custom gesture recognizer if you want, too):

    - (void)handlePanSuper:(UIPanGestureRecognizer *)sender
    {
        static UIImageView *viewToMove;
        static CGPoint      originalCenter;
    
        if (sender.state == UIGestureRecognizerStateBegan)
        {
            CGPoint location = [sender locationInView:self.view];
    
            if (CGRectContainsPoint(self.image01.frame, location))
            {
                viewToMove = image01;
                originalCenter = viewToMove.center;
            } 
            else if (CGRectContainsPoint(self.image02.frame, location))
            {
                viewToMove = image02;
                originalCenter = viewToMove.center;
            }
            else
            {
                viewToMove = nil;
            }
    
            if (viewToMove)
            {
                viewToMove.alpha = 0.8;
                [viewToMove.superview bringSubviewToFront:viewToMove];
            }
        }
        else if (sender.state == UIGestureRecognizerStateChanged && viewToMove != nil)
        {
            CGPoint translation = [sender translationInView:self.view];
    
            viewToMove.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
        }
        else if ((sender.state == UIGestureRecognizerStateEnded || 
                  sender.state == UIGestureRecognizerStateFailed || 
                  sender.state == UIGestureRecognizerStateCancelled) && viewToMove != nil)
        {
            // do whatever post dragging you want, e.g.
            // snap the piece into place
    
            [UIView animateWithDuration:0.2 animations:^{
                CGPoint center = viewToMove.center;
                center.x = round(center.x / 50.0) * 50.0;
                center.y = round(center.y / 50.0) * 50.0;
                viewToMove.center = center;
                viewToMove.alpha  = 1.0;
            }];
    
            viewToMove = nil;
        }
    }
    

    If I've misunderstood your question, let me know.