Search code examples
iphoneuiimageviewuitouch

How to get which object began touch in iphone


I have one view in iPhone application. In that view i added two UIImageView lets say img_view1, img_view2. Now i put touch event on that view as following.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     // Some code are here
}

how do i can get which image view began to touch ?


Solution

  • UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self];
    
    if (CGRectContainsPoint(img_view1.frame, pt)
    {
        NSLog(@"Image View 1 touched");
    }
    

    etc.