Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
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
 

Attachments

  • Screen Shot 2012-01-24 at 1.34.07 AM.png
    Screen Shot 2012-01-24 at 1.34.07 AM.png
    206.4 KB · Views: 161
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.
 
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.


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
 

Attachments

  • Screen Shot 2012-01-24 at 9.22.58 AM.png
    Screen Shot 2012-01-24 at 9.22.58 AM.png
    293.1 KB · Views: 180
Last edited:
Well, you've shown the connections for a newly-added button. But what about your images?

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.
 
Well, you've shown the connections for a newly-added button. But what about your images?
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"]];
}
 
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"]];
}

But they dont work though
 
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:

A gesture recognizer has one or more target-action pairs associated with it. If there are multiple target-action pairs, they are discrete, and not cumulative. Recognition of a gesture results in the dispatch of an action message to a target for each of those pairs. The action methods invoked must conform to one of the following signatures:

Code:
- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

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.)
 
Last edited:
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.)

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
 
Re-read this part that ArtOfWarfare quoted:
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..... ?
 
Last edited:
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.
 
Alright, I think I have to go for a UIButton rather then UIImageView.Thanks for all your helps...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.