collectionView(_:validateDrop:proposedIndexPath:dropOperation:)
Validates whether a drop operation is possible at the specified location.
Declaration
@MainActor optional func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: any NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperationParameters
- collectionView:
The collection view asking you to validate the drop operation.
- draggingInfo:
The information about the drag operation.
- proposedDropIndexPath:
The index path at which the drop would occur. This parameter is passed by-reference and can be modified to change the proposed index path.
- proposedDropOperation:
The type of drop operation being proposed. This parameter is passed by-reference and can be modified to change the drop operation type.
Return Value
A value that indicates which dragging operation to perform. Return NSDragOperationNone to disallow a drop at the proposed location.
Discussion
Although implementation of this method is optional, you must implement it to support drops onto the associated collection view. You must also call the collection view’s registerForDraggedTypes(_:) method to register the types of drops it supports. If you do not perform both of these actions, the collection view does not accept drops.
When an interactive drag operation occurs, the collection view calls this method to determine whether the current mouse location is a valid place to drop the content. This method may be called many times during the course of the drag operation. Your implementation should look at the proposed location and return a constant that reflects how the drop would be handled.
While validating the drop location, you can suggest a better drop location by updating the values in the proposedDropIndexPath and proposedDropOperation parameters. For example, you might suggest dropping the content before the specified item instead of on it. The collection view sets the proposedDropOperation parameter to NSCollectionView.DropOperation.on when the mouse is closer to the middle of an item than to its edges; otherwise, it sets the parameter to NSCollectionView.DropOperation.before.
See Also
Drag and Drop Support
Supporting Collection View Drag and Drop Through File PromisescollectionView(_:canDragItemsAt:with:)collectionView(_:pasteboardWriterForItemAt:)collectionView(_:writeItemsAt:to:)collectionView(_:namesOfPromisedFilesDroppedAtDestination:forDraggedItemsAt:)collectionView(_:draggingImageForItemsAt:with:offset:)collectionView(_:draggingSession:willBeginAt:forItemsAt:)collectionView(_:draggingSession:endedAt:dragOperation:)collectionView(_:updateDraggingItemsForDrag:)collectionView(_:acceptDrop:indexPath:dropOperation:)