cocoanscollectionviewnscollectionviewitem

Drag selecting in NSCollectionView from inside of items


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.


Solution

  • 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;
    }