I’m not asking for givemesamplecodenow responses, just a nudge in the right direction would be much appreciated. My searches haven’t been any good.
I have a UIButton that I am able to move freely around the screen.
I would like to limit the drag area on this object so it can only be moved up and down or side to side. I believe I need to get the x,y coordinates of the boundaries and then restrict movement outside this area. But that’s a far as I have got. My knowledge doesn’t stretch any further than that.
Has anyone implemented something similar in the past?
Adam
Presumably you'd be using
touchesBegan:
,touchesMoved:
, etc., so it should be as simple as testing whether the touch point is outside your view's bounds intouchesMoved:
. If it is outside, ignore it, but if it's inside, adjust the position of the button.I suspect you may find this function useful:
So let's say you're in the middle of the drag operation. You're moving the button instance around by setting its center to the center of whatever gesture is causing the movement.
You can impose restrictions by testing the gesture's center and resetting the center values if you don't like them. The below assumes a button wired to an action for all Touch Drag events but the principle still applies if you're using gesture recognizers or touchesBegan: and friends.
If you want a button that slides only on one axis, that's easy enough:
Or perhaps you want the button draggable only inside the region of a specific view. This might be easier to define if your boundaries are complicated.