Bhavin_m
Bhavin_m

Reputation: 2784

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 ?

Upvotes: 0

Views: 568

Answers (4)

user529758
user529758

Reputation:

UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];

if (CGRectContainsPoint(img_view1.frame, pt)
{
    NSLog(@"Image View 1 touched");
}

etc.

Upvotes: 2

Nims
Nims

Reputation: 431

use this code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

   UITouch *touch = [touches anyObject];
        if(touch.view == img_view1)
        {
    }
    else if(touch.view == img_view2)
        {
    }
}

Upvotes: 0

Mrunal
Mrunal

Reputation: 14128

Use this code

UITouch *touch = [touches anyObject];

if (touch.view==imageView1) { 
  // do something
}
else if (touch.view==imageView2) { 
  // do something else
}

Hope this is what you are looking for.

Enjoy coding :)

Upvotes: 0

Vikas S Singh
Vikas S Singh

Reputation: 1766

Make your image view user interaction enabled in the xib (or through the code if you are adding it programmatically) and use the UIResponder methods touchesBegan, touchesMoved, touchesEnded etc to detect the touch on the image view:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

if ([touch view] == yourImageView)
{
        //add your code for image touch here 
}

}

Upvotes: 0

Related Questions