views:

826

answers:

2

Ok I have 6 custom UIButtons. Their normal state image are all circles images. They are all spaced out equally but all the circles touch each other.

The problem with the custom UIbutton (which has a circle image on it), is that the hit area of that button is square, and the corners of this square overlaps the hitarea of the other custom button's hitarea.

How do i make the hit area of a UIbutton whos normal state has a circle image, be only clickable on that circle only, rather than the normal square hit area?!

I hope that someone can find a way for me to solve this problem that i currently am having!

Thanks in advance

Pavan

+2  A: 

If the square area of a "circle" is clicked on, you should then check if the distance between the center of the circle and the coordinates of the click are less than the radius of the circle. If not, then the "hit" can be ignored.

Demi
Firstly, thank you very much for the fast response, i am literally waiting for answers right now, since im working on a project!I will try this, straight away. It makes sense.ok so that takes care of the circles, what about complex shapes like a triangle? i have two of those?That's pretty much it.
Pavan
oh and by the way, when the user clicks on the custom button, a sound gets triggered. So using your suggestion, if i check if the coordinates are within the circle, and it happens that the user was not in the circle itself but was in the hit area, because its a UIButton it will still go to the highlight state.....so how do i also tell the program to ignore going to the highlight state?! thanks in advance
Pavan
@Pavan: regarding triangles, the math code to check that the coordinates are within the lines described by the three vertices, or, perhaps even easier is to check if the coordinates are outside of any line described by two vertices and ignore on that case. This should be simple - for any given coordinate x you should be able to calculate the expected y value for the triangle's edge, and then make a comparison with the y value of the click. I hope this makes sense.
Demi
@Pavan: Regarding the second question - I haven't played enough with UIButton to know this, but I assume that there is a way to capture/hook into the click event (or touch event). I would manage the logic that checks if it is in the region and controls the highlighting (from UIControl) in that code. Just a guess...
Demi
A: 

Thanks for the input demi, I just basically put hidden buttons on top of the images. They weren't really circles, they were more complex shapes; hence the reason I didn't bother about the maths, so I just placed hidden buttons on top of the images themselves and make those hit areas and cover appropriately until I get the desired hit area coverage I need.

Although not very neat, it works well! :D

Thanks once again for the swift reply demi.

Pavan