Help in detecting a touch in UIImageViews

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

  1. adildacoolset macrumors 65816

    Sep 5, 2011
    Lusaka, Zambia(If you know where it is)
    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:

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch* touch = [touches anyObject];
        if ([touch view] == self.imageView1){
            [self doAMethod];
        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
    statement does not do anything, signifying that it didn't respond to the touch at all.
  2. dejo Moderator


    Staff Member

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


    Sep 2, 2008
    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