How to get the UIImageView tag that I click?

I have several UIImageView, each of which has a tag; and I have an array of images, what I want to do: when the user clicks one of the UIImageView, the application returns a specific image from the array.

i implement this:

- (void)viewDidLoad { [super viewDidLoad]; scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; [self.view addSubview:scroll]; NSInteger i; for (i=0; i<8; i++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)]; imageView.backgroundColor = [UIColor blueColor]; imageView.userInteractionEnabled = YES; imageView.tag = i; NSLog(@"%d", imageView.tag); [scroll addSubview:imageView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(findOutTheTag:)]; [imageView addGestureRecognizer:tap]; } scroll.contentSize = CGSizeMake(320, 115*i); } - (void)findOutTheTag:(id)sender { // HOW TO FIND THE tag OF THE imageView I'M TAPPING? } 

I want to know imageView.tag and pass imageView.tag to

 UIImageView *tappedImage = [imageArray objectAtIndex:imageView.tag]; 

to display the image.

I marked them all, the question is, how can I recognize the tag image that I click? thanks for reading ^ _ ^

+6
iphone tags uigesturerecognizer
source share
3 answers

At the risk of making a recommendation without seeing the full picture of your application, why not use a custom UIButton instead of UIImageViews for this? With UIButton, you can configure the action and pass the sender ID, from which you can easily access your tags and pull data from your array.

OR, if you really want to use the code above and know that the fact is the void findOutTheTag: (id) method is called, all you have to do is:

 - (void)findOutTheTag:(id)sender { switch (((UIGestureRecognizer *)sender).view.tag) { case kTag1: //... case kTag2: //... } } 
+12
source share

Instead of using UIImageView, why don't you use UIButton. That way you can just add a listener for UITouchDown events. You can mark each button so that in your touchDown method you can find which button was pressed.

  UIButton *button = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)]; button.backgroundColor = [UIColor blueColor]; button.tag = i; [button addTarget:self action:@selector(touchDown:) controlEvent:UIControlEventTouchDown]; 

And inside the touchDown method: you just need to send the sender to UIButton in order to access the tag.

 - (void)touchDown:(id)sender { UIButton* button = (UIButton*)sender; switch(button.tag) { case TAG1: break; //etc } } 
+2
source share

To find out which image to touch, use the touchBegan method:

Note. First of all, you need to confirm that the image must be userIntrectionEnabled=YES; Now use this method:

 -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{ // get touch event UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; if ([touch view].tag == 800) { //if image tag mated then perform this action } } 

You can use the switch statement inside touchBegan .

+1
source share

All Articles