Reputation: 42
I got this solution on this site: Click an UIImage and open an UIImageView in Objective-c
Add UITapGestureRecognizer
to your UIImageView
:
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourSelector)];
[thumbnail addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
thumbnail.userInteractionEnabled = YES; // very important for UIImageView
This is working very fine for single ImageView, but I am adding more than one (about to 20) to my scrollView then How can I differentiate which ImageView will tapped or selected by user. I tried to set my own @selector(imageClicked), but it only returns tag for last imageView.
I am adding addGestureRecognizer in a loop, as I load 20 static images dynamically in an imageView.
Upvotes: 1
Views: 7391
Reputation: 1965
This might help
for(int i=0;i<20;i++)
{
UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourimage.png"]];
[img setTag:i];
img.frame= //set frame accordingly;
img.userInteractionEnabled = YES;
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[img addGestureRecognizer:tap];
[tap release];
[scrollView addSubView:img];
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
UIImageView *imageView = (UIImageView *)recognizer.view;
switch([imageView tag])
{
case 1:
//do your work
break;
.
.
.
.
case n:
}
}
Upvotes: 5
Reputation: 393
Please try to subclass the ImageView & add the gesture recognizer to the subclass.
Now for each image create the ImageView object and add the image to that object. Set some unique property so that you can identify which object click like the name of the image.
Upvotes: 0
Reputation: 12081
You cannot add a single tap recognizer to multiple views. Create a new one for every view you want to add a tap recognizer to. Since you are using a tableview just do that in the tableView:cellForRowAtIndexPath:
method:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// usual implementation
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeue ...];
if (!cell) {
cell = [[UITableViewCell alloc] init....];
// add new gesture recognizer here.
}
// setup cell: set the image (just an example)
cell.imageView.image = [images objectAtIndex:indexPath.row];
return cell;
}
Instead of using tags like mentioned in the other answers, and just getting the imageview try to work with the underlying model. When handling the tap, find the indexPath to know what model object to access:
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
UIImageView *imageView = (UIImageView *)recognizer.view;
// assumes the image view is direct subview of the cell
// change to match your cell structure
UITableViewCell *cell = (UITableViewCell *) [imageView superview];
// get the index path for the cell clicked
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// TODO: Use index path to get full image to display
}
This way you know the exact row of the image clicked, so you can access your model to get access to the full image to display.
Upvotes: 1
Reputation: 916
You need to add tag to all images - thumbnail.tag = 100 for example.Then modify your selector to youSelector:(UITapGestureRecognizer *)sender; In selector add switch
- (void) yourSelector:(UITapGestureRecognizer *)sender {
UIImageView *imageView = (UIImageView *)sender.view;
switch(imageView.tag) {
case 100: {
//This code will be handled if tag == 100;
}
}
}
Upvotes: 0
Reputation: 3927
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
for (UIImageView *thumbnail in imageArray) {
[thumbnail addGestureRecognizer:tapRecognizer];
}
[tapRecognizer release];
You can get the view from the property "view" of UIGestureRecognizer. In your selector, for example:
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
UIImageView *imageView = (UIImageView *)recognizer.view;
// Now do something with your view
}
Upvotes: 1