2

I have a single imageview with a static country map divided into regions of that country. What I'd like to do is detect the touch location on the image and provide content about the corresponding region. Since region borders are not linear, how can I save each region's area? Do I need several imageviews (or even custom UIButtons with those images) each belonging to one region or is it possible the way I'd like?

This is the first thing that came to my mind so maybe there is a simpler and better way which I'd love to hear about and I couldn't know how to search for this so apologies if there is a duplicate. And of course I'd appreciate the help.

Thanks

Eren Beşel
  • 1,047
  • 8
  • 16

2 Answers2

2

One simple & exact way would be to use Ole Begemann's OBShapedButtons - one for each state.

This will allow you to detect exactly which state was selected. Simply put image of each state in separate buttons (with transparent surroundings) and align buttons next to eachother so that state-borders allign one to another.

Buttons will detect the location of the press and if one button was pressed on its transparent region the touch will be passed along until the correct button gets it.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
1

The simplest way to do something like what you want would be to just put some UIButtons over the top of your UIImageView, making their type custom (so they are transparent). Generally fill in the area of each country with UIButtons. If you test your app, you will probably notice that people will touch the center of each country, so I wouldn't worry about getting 100% coverage. Depending on the shape of the countries, one or two square UIButtons would probably be enough.

If you did want to go the 100% coverage route, you could embed each country in a separate UIImageView subclass, and when you detect a touch anywhere, go through each country image and see if the point touched isn't transparent. When you hit that (a non-transparent pixel) you have found the country. See this post for relevant code: How to get the color of a pixel in an UIView?

Community
  • 1
  • 1
Ned
  • 6,280
  • 2
  • 30
  • 34
  • Thank you Ned, this is also basically what thought would be the easiest (your first para.). If no other suggestions come, I'll accept your answer. – Eren Beşel Mar 20 '12 at 16:46