iOS : detect UIImageView for UITouch Events

11,194

Solution 1

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch=[touches anyObject];

    if([[touch valueForKey:@"view"] isKindOfClass:[UIImageView class]])
    {
            UIImageView *viewSelected=(UIImageView *)[touch valueForKey:@"view"]; //it returns touched object
            //for further differences can user viewSelected.tag
    }
}

Solution 2

Here you go:

this is only for one imageview you can detect the other by the same if statement.

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

    if (![imageView pointInside:touch_point withEvent:event]) 
    {
        NSLog(@"point inside imageview");
    }
} 

or you can also do this :p

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if (touch.view == iv)
    {
        NSLog(@"i got you");
    }
}

like this: (iv and iv2 are 2 different UIImageView`s)

if (touch.view == iv)
{
    NSLog(@"i got you");
}

if (touch.view == iv2)
{
    NSLog(@"i got you too :p");
}

Solution 3

Code to get only the X and Y coords from the UIImageView:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [[event allTouches] anyObject];

   CGPoint touch_point = [touch locationInView:self.imgView];

    if ([imgView pointInside:touch_point withEvent:event])
    {
        NSLog(@"point inside imageview");
        cords=[NSString stringWithFormat:@"%f,%f",touch_point.x,touch_point.y];
        NSLog(@"cords are%@",cords);
    }
}
Share:
11,194
Hemang
Author by

Hemang

Since April 2020 I started working on typescript, angular and ionic platforms. iOS Developer since 2012 | Download my app - Nastromy. I have created few repositories to help me and iOS community. You can check these repositories. If you want to learn more about me, this is my『email』.

Updated on June 13, 2022

Comments

  • Hemang
    Hemang about 2 years

    I've added some UIImageView dynamically and filled it with different images, what I am trying to do is "Allow user to set position for any UIImageView", for that I used

    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        //Here I want the object of particular UIImageView on which user touched.
    }
    

    In that method I'm doing,

    NSLog(@"%@",[touches anyObject]);
    

    It returns output

    <UITouch: 0x68b95e0> phase: Began tap count: 1 window: <UIWindow: 0x68875d0; frame = (0 0; 320 480); layer = <UIWindowLayer: 0x68b6470>> view: <UIImageView: 0x6a74cf0; frame = (83.7763 83.7763; 182.447 182.447); transform = [0.968912, -0.247404, 0.247404, 0.968912, 0, 0]; alpha = 0.8; opaque = NO; tag = 3; layer = <CALayer: 0x6a74980>> location in window: {161, 230} previous location in window: {161, 230} location in view: {52.7761, 105.448} previous location in view: {52.7761, 105.448}
    

    Note, in above output, it showing my UIImageView object on which I touched. But I want that object from it!!!

    I want my UIImageView on which user touched?, I have already set property userInteractionEnabled=YES so the problem isn't with it!

    I used below code to get it so, but it wont work.

    NSInteger tag=[[[touches anyObject] view] tag]; //It only returns tag of UIView tag
    

    I Google it but doesn't come with solution!

    Thank you in advance for any help!

  • arcady bob
    arcady bob about 7 years
    I believe option 1 works but option 2&3 don't. touch.view returns the UIView that the UIImageView is in, not the UIImageView itself.