Quote:
Originally Posted by juanchofern
Hello I would like to do an array of uiimageviews and when i touch an uiimageview i would like to know which uiimageview has been touched.
I would like to get the index.
Does anybody how to do it?
Thanks in advance!!
|
The easiest way to do this would be to create a grid or row of UIButtons with their type set to custom, and install your UIImages from the array into each button. Set a tag on each button that corresponds to the array index. The view controller's viewDidLoad code could loop through using [[self view] viewWithTag: index] to finding each button and installing the images.
Then link all the buttons to the same IBAction that would use [sender tag] to get the index of the image that was tapped.
You could either set up your grid of buttons in IB in advance, or write code to lay them out.
If you don't use buttons, you could create a custom subclass of UIImageView that has userInteractionEnabled, and has a touchesBegan and/or touchesEnded method. Set each up with a delegate. The touch methods would generate a message to the delegate telling the delegate which image was touched. You'd need some logic in the view controller that would install the images into each custom image view.
Alternately you could create a custom subclass of UIView that would create a grid of UIImageView objects inside it, install the images from your array into those image views, and have that custom view handle touch events. It would have to do math on the coordinates of the touch to figure out which image was touched.
All of these approaches would be much easier if you lay out the images so each one takes up the same amount of space.