Search code examples
iosuitouch

How to called TouchesMoved for 2 different UIImageView


Hello I have 2 UIImageViews in UIView.

Once I touch on UIImageView touchesBegan method gets called. But once I drag on UIImageView then touchesMoved is called. But at the same time touchesMoved for the second UIImageView is also called.

Can you please help me how i can get touchesMoved event for both the UIImageViews?

This is my code

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

    if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv1 Begin");
    if (CGRectContainsPoint(iv2.frame,currentPoint)==YES)
        NSLog(@"iv2 Begin");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    if(CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv1 Moved NO");
    if(CGRectContainsPoint(iv2.frame,currentPoint)==NO)
        NSLog(@"iv1 Moved YES");
    if(CGRectContainsPoint(iv2.frame,currentPoint)==YES)
        NSLog(@"iv2 Moved NO");
    if(CGRectContainsPoint(iv2.frame,currentPoint)==NO)
        NSLog(@"iv2 Moved NO");
}

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

    if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv1 End");
    if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv2 End");
}

Solution

  • You can use two outlets linked to two views and this is the code for reacognize the two view inside touch methods:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       UITouch *t = [touches anyObject];
       touchedView = t.view;
       if (t.view == view1) {
    
        //todo something with view1;
    
       } else if (t.view == view2) {
    
          //todo something with view2
    
       }
    }