I am moving a few CALayers using the -mouseDragged
method, and now I would like to "snap" them when they are sufficiently near (or when they are overlapping just a little bit).
Each layer is not a "square": I am drawing different polygons.
I thought that a way to do this is to:
- get the position of the layer being moved;
- get the overlapping layers or the layers that are near the layer being moved;
- On each side of the polygon in which (2) is true, I need to check the maximum distance (at right angles to the side of the layer) between the side of the moving layer and the side of the layer that is "near" (this is a negative value when the layers overlap).
- move the layer accordingly.
I don't know if this is a correct approach. The first thing that comes in my mind is:
- What happens if I can "snap" in more than one side?
And, even if I try this way, I have no idea what to do regarding (2) and (3).
Is there a better way to do that?