I’ve looped through an NSArray of NSDictionary objects that contain the data for several images to be displayed. This data is then converted into UIImageView objects and added to the current view as subviews. Simple enough! All the images passed in via the array/dict are displayed on the screen as desired, and it works great so far:
for (NSDictionary *caseData in caseImages) { // caseImages is the array
UIImage *caseImage = [UIImage imageNamed:[caseData objectForKey:@"image_file"]];
UIImageView *caseView = [[UIImageView alloc] initWithImage:caseImage];
caseView.frame = CGRectMake([[caseData objectForKey:@"position_x"] intValue], [[caseData objectForKey:@"position_y"] intValue], [[caseData objectForKey:@"size_w"] intValue], [[caseData objectForKey:@"size_h"] intValue]);
[self.view addSubview:caseView ];
}
However, now I need to take this a step further, and implement a touchUpInside event handler that can be attached to each of these UIImageView objects if possible, in such a way that the handler method can further animate/manipulate each UIImageView, ideally centering and enlarging the touched image, and simply fading the rest of the images out.
Where I’m stuck is on how to successfully add the touch events, with the ability to reference the image that case touched, as well as the rest of the untouched images if possible, and then apply the proper transition animations to each image respectively.
I have seen examples similar to the snippet below, but I haven’t been able to get it working:
[caseView addTarget:self action:@selector(spotlightCase) forControlEvents:UIControlEventTouchUpInside];
Although, had I actually gotten it to work, I’d still feel a bit left in the dark about how to successfully reference each and every UIImageView created from the loop, so each image could be animated/manipulated properly according to which one had indeed been touched. Would I need a new array property in my view controller that would hold the pointers for each UIImageView object added to the view? Is none of this even accomplishable with UIImageViews? I saw someone mention using custom UIButton objects, but I thought that might be equally complicated/impossible seeing as I also need to animate the size and location of the images anyway.
Any chance you can shed some insight on how to implement all of the tricky endeavors above?
Thanks!
One way to do this is to add a UIButton to your view controller’s view for each image and set the backgroundImage property to the
[UIImage imageNamed:]inside your loop. You can set a generic selector to the target of each button just like in your second snippet. I have used a similar trick before and set the tag property of the button to identify the index of the button that was pressed. If you also add a colon to the end of your@selector(spotlightCase:)your method will also get passed the id of the object that called it. Your method definition would be:This is where you can use the
sender.tagto control your logic.Hope this helps
Dave