3
votes

I have a NSCollectionView implemented and working fine. However, when dragging items around to re-arrange them, I'd like to accept drags only between objects.

The current behaviour is that you can drag between objects (it opens an space) but also over an object. I hope the images below speak better. Dragging an item over #2, for example, will put it before #2. I'd like to disable this and only accept drags in between.

In the images below:

  1. The original state
  2. Item #0 dragged between #1 and #2
  3. Item #0 dragged over #2 (the result is the same)

drag and drop

1
Does your collection view have a delegate?Willeke
@Willeke Yes, of course.sidyll
Can you show your code where you drag, or something is relevanttuledev
@anhtu Yes, but I don't know if it makes any difference. It's very generic and big implementation of standard drag-and-drop operations. I'm not really asking about how to fix something, just how to implement :-) any specific part you need to see?sidyll
Why don't you detect it by index(indexPath.row or something similar) of cell? Or using CGRectIntersectsRect, just accept if it intersect with >= 2 cell in a row. Or using position of each cell to detect between, over. I think you already think about them. I just want to know the reason why don't?tuledev

1 Answers

4
votes

So, after some more research, I found that the validadeDrop method has a parameter that describes exactly that, if the operation is over or in between. And since it is given as a reference, one can change the parameter to overwrite a certain behaviour. So, the solution is to add this check to the method:

- (NSDragOperation)
    collectionView:(NSCollectionView *)collectionView
      validateDrop:(id<NSDraggingInfo>)draggingInfo
     proposedIndex:(NSInteger *)proposedDropIndex
     dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation
{
    /* ... */

    if (*proposedDropOperation == NSCollectionViewDropOn)
        *proposedDropOperation =  NSCollectionViewDropBefore;

    /* ... */
}