views:

38

answers:

2

I am thinking about creating a Game Board view with an array of subviews to form slots for tiles. I plan to have tiles that I can drop onto the board and have them snap into position. The part I am unclear about is when I drop my tile and the touchesEnded event fires, what is the best way to loop through the subviews of my Game Board to see which slot I am over so I can have the tile snap into proper position? Or is there a better way to keep track of all the "slot" positions?

I really don't want to have to hardcode every cell position and then keep track of it if the Game Board ever gets shifted in my view controller.

+1  A: 

Check out hitTest:withEvent: and pointInside:withEvent:.

imaginaryboy
The trouble I am currently having is the hitTest:withEvent keeps pointing to the tile uiview that I drop. I want it to remain interactive because I need to drag and drop it but since it is on top the HitTest says that is the piece that received the hitTest.
SonnyBurnette
Well one option could be to cycle over the possible targets using pointInside:withEvent:, no? Though from your comment about about the layout of the board, it seems like based on the point you can just calculate the target directly, that is, based on the x of the drop point you know exactly which column of your single row that applies to.
imaginaryboy
That's the route I am going to take. Thanks for the help.
SonnyBurnette
A: 

ditto imaginaryboy. you don't need to loop through anything. let UIKit do the heavy work for you. Compute the center point of the piece that was dropped on your board and the use hitTest:withEvent: on the view containing your board.

Engin Kurutepe