Help in detecting a touch in UIImageViews

Discussion in 'iOS Programming' started by adildacoolset, Feb 9, 2013.

  1. adildacoolset macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #1
    I'm trying to detect a touch in a UIImageView. I'm trying to make it such that if a user taps on an image, it wil call a method, and if a user taps on another, it will call another method. Here is my code so far:

    Code:
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch* touch = [touches anyObject];
        
        if ([touch view] == self.imageView1){
            [self doAMethod];
            NSLog(@"Touched!");
        }
        else if ([touch view] == self.imageView2){ 
            [self doAnotherMethod];
        }
        else if ([touch view] == self.imageView3){
            [self doYetAnotherMethod];
        }
        else if([touch view] == self.imageView4){
            [self doADifferentMethod];
        }
    }
    
    
    Just to let you know, I ticked the userInteractionEnabled box for the images in the IB.

    So far, it doesn't work. Please help. Even the
    Code:
    NSLog
    statement does not do anything, signifying that it didn't respond to the touch at all.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Try putting an NSLog (or better yet, a breakpoint) before your conditionals, to verify your touchesBegan: is being called at all.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    The UIResponder touches methods are not used much anymore. What you describe sounds like a button. If so then use a button with an image. Otherwise use a UIGestureRecognizer for this.

    Using buttons or gesture recognizers will make this task simpler.
     

Share This Page