UIButton with a custom asymmetric clickable region? - ios

I have an image with various asymmetric regions, is it possible to place a button above each region?
The image will be something similar to this: https://cdn.dribbble.com/users/1557638/screenshots/4367307/proactive_d.png

After way too much research, I’ve decided to go with following solution for an image with multiple asymmetric clickable regions:
Duplicated the image file, colored each region with a different color, and stored as an asset.
Placed a simple tap gesture recognition over the displayed image. When tapped, I get the relative coordinate to the displayed image.
Get the color from the map image at the tapped coordinates.
Compare to a predefined enum.

Related

How to recolor an image on specific area?

I'm trying to create an iOS recoloring app (this is my reference), and i need to know how recolor some portion of the image when user taps on a given area. All the loaded pictures will be black/white initially.
Is there any prebuilt library? Or which graphics framework should i use?
Any help will be appreciated.
If what you are looking for is adding/replacing the colour within a certain shape and edges are really important (as in the example) then you should be looking into vectorised drawing.
What this means is every shape in your image would have an actual object representation in your code, and you could easily interact with that object to do whatever you want (i.e. tap gestures to change colour, zoom etc.).
This however, means that you can't simply use .jpeg images, and you need to use images in vector format, such as .svg or CorelDraw.
As a reference, check out SVGKit, which is an excellent library for working with SVG images.

iOS Detect Touch on Custom Map Image to Select Irregular Shaped Region

I have a png image of a country's map showing its regions.
I want to detect touch and select (uniquely identify) any irregular shaped region from map and perform some activity for selected region. I also want to show/display selection of region while tapping on it (either by highlighting region's image or showing a line on his boundary).
Please tell me the best way to achieve this. MKMapView won't help as i don't want to use Apple/Google maps.

Fill image with different color by detecting the different parts

I have an Image of a landscape which i need to fill with different colors.
When i select colors from palette and start scrubbing on any particular part, only that part should get the color even if by mistake i take my finger outside of that image part.
So basically i need to detect which part of image have i tapped so that only that part takes the color.
I am developing this app in Cocos2dx, but any help in logic would be a good point to start.
Here is an example of what i want.
Note : I know i could achieve this by taking separate images and then detecting touches, but that increases the app size by alot of MB's.
I guess user will be able to draw only on white part of the image.
If above is true, what i want you to do is, in your touchesMoved method, check if any black color (non white) pixel is present between previous touch point and current touch point.
If there is no such black pixel, then draw it else dont draw it.

Is it possible to construct an irregularly shaped button that would be an overlay on a map?

The idea is as follows: the view contains a map (can be a zoomable image), map is divided into regions, each region is touchable and takes you to a more detailed view (not a map).
I'm not asking for solutions I'm asking for a general direction, of course if something like this is possible at all.
You have to create transparent images and change hittest method to not account for transparent region. For details see this link.
You can also see this project in github.

Detect touch coordinates on a sinle UIImageview

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
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.
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?

Resources