can not detect a touch for UIImageView, scrollView

Discussion in 'iOS Programming' started by tranvutuan, Jan 23, 2012.

  1. tranvutuan macrumors member

    Joined:
    Dec 19, 2011
    #1
    Hi everyone
    I have just added 3 UIImageView in a scrollView but I cant detect a touch for each of UIImageView...
    I have attached an img for my StoryBoard so that you can easily see my issue. Please help if you have any hints for this. Thanks
     

    Attached Files:

  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I can easily see you neglected to show us the right panel, namely the connections tab.

    Edit:

    Although wouldn't it be a lot easier to just use a UIButton? You can assign an image to a button so that visually it looks the same as a UIImageView, but it comes with the ability to detect touches "built-in", that is, without you having to write any code to have it detect touches.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  4. tranvutuan, Jan 24, 2012
    Last edited: Jan 24, 2012

    tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #4

    Yeah you are rite.. just gave it a try and it is easier then.. However, i would like to try out with UIImageView after all.By the way, I did attach an img with connections tab
     

    Attached Files:

  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Well, you've shown the connections for a newly-added button. But what about your images?
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    I think showing connections with your controller would be better... that way we'd see all the connections between it and your buttons and images.
     
  7. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #7
    Others are UIImageView and they are not supported for touch event.. Therefore, I have to add in the codes like below
    Code:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        myScrollView.contentSize = CGSizeMake(500,0);
        UITapGestureRecognizer *touch;
        touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(test:)];
        [myImgView1 setUserInteractionEnabled:TRUE];
        [myImgView2 setUserInteractionEnabled:TRUE];
        touch.numberOfTapsRequired = 1;
        [myImgView2 addGestureRecognizer:touch];
        [myImgView1 addGestureRecognizer:touch];
        
        myImgView1.tag = 8 ;
        myImgView2.tag = 9 ;
        
        
        [myButton setImage:[UIImage imageNamed:@"dresses.png"] forState:UIControlStateNormal];
        [myButton setImage:[UIImage imageNamed:@"dresses.png"] forState:UIControlStateHighlighted];
    
    }
    - (void) test:(UIGestureRecognizer *)sender
    {
        UIImageView *myImg  =   (UIImageView*)sender.view;
        if (myImg.tag == 8 ) {
        //NSLog(@"fdfdfdfdfdfdfdf %d",((UIImageView*)id).tag);
            NSLog(@"JUST TOUCHEDDDDDD MYIMGVIEW1");
            [myBigImgView setImage:[UIImage imageNamed:@"button"]];
        }
        else if (myImg.tag == 9 )
            NSLog(@"JUST TOUCHEDDDDDD MYIMGVIEW2");
        [myBigImgView setImage:[UIImage imageNamed:@"dresses"]];
    }
     
  8. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #8
    But they dont work though
     
  9. ArtOfWarfare, Jan 25, 2012
    Last edited: Jan 25, 2012

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    Please use the "Edit" button at the bottom of your post to add something. Do not make two back to back posts.

    Regarding why your code isn't being called, you don't appear to have a delegate set up for your gesture recognizer. I'm not sure if that'll fix it but that's something I'm noticing...

    Edit: (See, you should edit like this?)
    Nevermind, you don't need a delegate... still looking...

    Found this in the class reference:

    So maybe your issue is that the method must be handleGesture: instead of test:?

    Other thing, do you know whether test: is being called at all? It looks to me like if test: is called and the view's tag was neither 8 nor 9, it won't do anything at all. Either add an NSLog that lets you know it's being called, or set a breakpoint (a breakpoint would be quicker and easier.)
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Yes, they are. Anyways, not sure why you would make so much extra work for yourself as you seem to be trying to duplicate the functionality that UIButton already has built-in.
     
  11. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #11
    test method is being called but only for the latest UIImageView which invokes test
    Code:
        [myImgView2 addGestureRecognizer:touch];
        [myImgView1 addGestureRecognizer:touch];
    this means test is being invoked for myImgView1 but myImgView2. It is odd, isn't it. When I do
    Code:
        [myImgView1 addGestureRecognizer:touch];
        [myImgView2 addGestureRecognizer:touch];
    myImgView2 is calling test, but myImgView1
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Re-read this part that ArtOfWarfare quoted:
     
  13. tranvutuan, Jan 25, 2012
    Last edited: Jan 25, 2012

    tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #13
    Yeah thanks for pointing out then. So what should we do if right now we are having 20 UIImageViews and we have to tell which one has been invoked. Does it sound like we are going to have 20 touches and each of them is mapped to each UIImageView then..... ?
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Why aren't you considering UIButtons?
     
  15. jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #15
    Use UIButton.

    If you really just hate buttons and everything they represent then at least make a subclass UIButtonImageHybrid rather than trying to beat down the UIImageView until it submits and acts like a button.
     
  16. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #16
    Alright, I think I have to go for a UIButton rather then UIImageView.Thanks for all your helps...
     

Share This Page