Search code examples
iosuiviewcoordinatesdragtouchesmoved

Drag UIVIew relative to finger


I have a little problem. I have some UIView and I'm able to drag them with methods touchesBegan and touchesMoved.

My problem is my UIView's coordinates (0,0) is coming under my finger. I would like to avoid that.

Do you know how to do that. I try some things but with no success :(

Thanks a lot !


Solution

  • It will depend on the size of the view but you can probably set the center property to your touch's location.

    Animating the center to your tap location

    [UIView animateWithDuration:0.25f
                          delay:0.0f
                        options:(UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut)
                     animations:^{
                         theView.center = tapLocation;
                     }
                     completion:NULL]
    

    Instead of just moving the location or translating it, you can animate the view to your current touch in all touches* methods.