1

I have a series of items that I am showing them in a NSCollectionView. The selection and multiple selection are both enabled.

The user can select items by dragging (i.e. marking items by drag). however this works when the user start dragging from collection view background or the space between items (and not on the items) but I want make it possible when dragging starts on the items too.

I want something like this photo if we consider text and image as a single item.

enter image description here

image source: http://osxdaily.com/2013/09/16/select-multiple-files-mac-os-x/

Thank you in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What have you done so far? – Willeke Feb 13 '18 at 11:22
  • The point is until this state most of the work is done through IB, and Datasource is just showing the items. What I want to achieve here is an extra feature which is not implemented by default in NSCollectionView. –  Feb 13 '18 at 11:40
  • What did you do to implement drag & drop? Show us your code. – Willeke Feb 13 '18 at 11:44
  • it's not drag and drop, it's drag selecting like finder app that you mark items, but I want to make it work when the user start the drag on items. It has to do with events but I can't figure it out how. –  Feb 13 '18 at 11:47

1 Answers1

3

Implement hitTest(_:) in the class of the item view to make the items "see through" for clicks. Return the collection view instead of the item view when the user clicks in the item view.

override func hitTest(_ point: NSPoint) -> NSView? {
    var view = super.hitTest(point)
    if view == self {
        repeat {
            view = view!.superview
        } while view != nil && !(view is NSCollectionView)
    }
    return view;
}
Willeke
  • 14,578
  • 4
  • 19
  • 47
  • It did the job very well, I appreciate the help –  Feb 20 '18 at 14:09
  • how can i say don't select an item unless the selection rectangle covers or intersect with the top half of the view? I can add this logic to your code but this works only for the first item that initiate the drag selection and when selection rectangle intersects with border of another item view, it gets selected. –  Feb 25 '18 at 09:13