Search code examples
uitouch

How to hold UIview and Move?


I wanted to hold UIView and Move like the way we all can change our icon on home screen.

How I can achieve this?


Solution

  • There's not a simple UI call that will do this for you.

    It would be possible to implement using the following sequence:

    • Detect a touch down event within the view.
    • Time the touch down period to reach a threshold where you're confident the user intends to move the view.
    • Once the threshold is passed, use some visual indication to indicate the view is moveable. This could be a animated wobble, like with the homescreen icons, or something more simple.
    • Track touch events as the user moves the view, and update its position accordingly to remain under the user's finger.
    • When the user lifts their finger (a touch up event), set the view's position to the new location (possible with some snap to grid, as used on the homescreen).
    • Detect some event to restore the view to its non-movable appearence. The homescreen uses the home button, but you could just use the touch up event.

    Hope that helps. There's quite a bit of coding required there, and much of it is specific to your UI and design choices.