user2361155
user2361155

Reputation: 93

drag and drop from uitableviewcell to uiimage view in ios

I need help implementing a drag and drop gesture, I would like to know Your opinion to find the best approach.

I have a view controller with a uitableview on the right and some image views on the left.

I would like to allow the user to drag every cell of the table view on one of the image views. Basically the drag-n-drop will fail if the cell is not released on one of the image views, otherwise the image view will change image according to the dropped cell.

What do You think is the best way to achieve this?

In addition, when the user start dragging, I would like that he drags around a particular shaped subview, with image and data, not drag the semitransparent cell.

Thanks,

Upvotes: 2

Views: 1945

Answers (1)

stevo-
stevo-

Reputation: 311

The basic steps for this would be:

  • Add a long press gesture recognizer to your table view (the whole view, not individual cells)
  • When the gesture begins, find which cell it is over (convert point, then user indexPathForRowAtPoint to get the cell)
  • Make a copy of that cell which will become the dragged view (take a snapshot, add it to a UIImageView and add that to your main view). Or you could make the view look however you want if you dont want it to look like the cell
  • make your original cell hidden so it looks like it is being dragged
  • On gesture changed, move teh position of this image view based on locationInView of the gesture)
  • On gesture ended, check to see if the cell is over an image (hitTest: for this) and do any animations or calculations you need.

Upvotes: 7

Related Questions