views:

412

answers:

1

How can I go about dragging a UILabel from point A to point B on my iPhone application?

+2  A: 

Basically, you would need to build the functionality yourself. You could do this by listening to touches in a superview that includes the full draggable area. The method hitTest:withEvent: can tell you if the touch down point is in the label to be dragged.

From there, you can override the touchesMoved:withEvent: method to update the position of the UILabel to keep it aligned with the finger. Updating the position of the label will automatically redraw it for you.

Here's a relevant question for iphone drag/drop.

Once you get the dragging working, I would also recommend paying attention to the location within the UILabel where the first touch was made, and handle the repositioning in such a way that the finger is always on that point within the UILabel. An easier-to-code but worse-looking version is to simply reposition, say, the upper-left corner of the label to be where the finger is, but this could make the label appear to jump when it first starts dragging.

It may sound intimidating, but it's really not that bad -- just a few lines of code in total. Unless you have views that might overlap. That's another few lines, depending on how you want to handle it.

Tyler
well views will overlap...
Matt S.
Once you have a handle to the label you want to move in the drag, you can call [self bringSubviewToFront:label]; and it will overlap everything else (if that's what you want to do).
Tyler