Determine how a user drags items from a UICollectionView?

I have a UICollectionView , and I would like to be able to touch and drag items up and down from the view and thus remove them. (Very similar to how the Dock works on OS X: drag and drop something and release it and it will be deleted).

I did some research, but almost everything I find is looking for CollectionViews that are being dragged to reorder. I don't need to reorder (I'm glad to just remove the element at the specified index from the source array and then reload), I just need to determine when the element is moved outside the view and released.

So, I suppose my questions are these:

1) Is this possible with the built-in CollectionView, a kind of itemWasDraggedOutsideViewFromIndex: method itemWasDraggedOutsideViewFromIndex: or something else?

2) If not, is it something that can be done with a subclass (and, in particular, is this possible for a beginner CollectionView)?

3) Are there any code examples or tutorials that you can recommend for this?

0
source share
2 answers

Here is a helper class that I worked on that does exactly that: implementation: https://github.com/Ice3SteveFortune/i3-dragndrop , hope this helps. There are examples on how to use it in TestApp.

UPDATE

In about a year, it will be a fully functional drag-and-drop infrastructure. Hope this proves useful: https://github.com/ice3-software/between-kit

+3
source
  • There is no built-in method that you suggest. What you want can be done, but you will have to process it using a gesture recognizer and appropriate code to handle the drag and drop operation.

  • I tried to use a subclass for this and finally came back to put it in my controller. In my case, however, I was dragging things to / from the collection view, as well as two other views on the screen.

  • I don’t know if you have a book, but the most useful thing I found is the Erica Sadun Core iOS6 Develper Cookbook, which has excellent drag and drop code in Collection Views. I don’t think that this specifically concerns dragging outside the CV, but for me the solution was to put the gesture recognizer in the general supervisor and always use its coordinates, not the coordinates of the subview.

One of the problems I ran into was that I wanted to be able to select cells with a tap, as well as drag and drop, and there is no way (despite Apple's documents opposite) to require that one gesture gesture not be executed in the view collection. As a result, I had to use a long press gesture to complete the whole operation, and there is no translation of InView for a long press (there is locationInView), so additional work is required:

iOS - gesture recognition converter translationInView

Another thing that will make it more difficult or simpler is the number of possible drops goals. I had many different views (straight UIView, collectionview and scrollViews). I found it necessary to keep a list of “drop targets” and check for intersections with targets when moving the drag object. One way or another, you should be able to determine if the view you are crossing is the place where the fall can occur.

If you are referring to a specific situation of dragging something from the view to remove it (for example, dragging to the trash can view), and this should not be difficult. You must remember that when you do the transformation, your frame becomes meaningless, but the center is still good; so you end up using the center for everything you usually use for the frame.

Here is the closest thing I found on the Internet that was useful; I have not finished using this class, although, as I thought, it would be too difficult to implement in my application.

http://www.ancientprogramming.com/2012/04/05/drag-and-drop-between-multiple-uiviews-in-ios/

Hope this was some help.

+3
source

Source: https://habr.com/ru/post/954816/


All Articles