I'm implementing the drag and drop in a collection view, for the most part it works as expected but I have this weird bug that when I click on a item and start dragging it the item appears outside outside the view and it off from my mouse cursor. I have no idea what is causing this.
This is how I accept the drop and call the func moveItem from the collection view.
indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
guard !indexPathsOfItemsBeingDragged.isEmpty else { return false }
var acceptDrop = false
for fromIndexPath in indexPathsOfItemsBeingDragged {
var modelUpdated = false
if fromIndexPath.section == indexPath.section { //moving items in same section
modelUpdated = dataSource.moveItemIn(section: fromIndexPath.section, fromIndex: fromIndexPath.item,
toIndex: indexPath.item)
} else if fromIndexPath.section != indexPath.section{ //move items between sections
modelUpdated = dataSource.moveItem(fromSection: fromIndexPath.section, fromIndex: fromIndexPath.item,
toSection: indexPath.section, toIndex: indexPath.item)
}
if modelUpdated { //notify collection view if model was updated
NSAnimationContext.current.duration = 0.25
collectionView.animator().moveItem(at: fromIndexPath, to: indexPath)
acceptDrop = true
}
}
return acceptDrop
}
This is how I set the constraints for my collection view.
`NSLayoutConstraint.activate([moviesCollectionView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
moviesCollectionView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
moviesCollectionView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
moviesCollectionView.bottomAnchor.constraint(equalTo: bottomAnchor)])`
And while I'm at it if somebody could elaborate a bit more on this delegate function.
pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
let pasteBoardItem = NSPasteboardItem()
pasteBoardItem.setString("\(indexPath.item)", forType: .string)
return pasteBoardItem
}
The drag and drop is only locally but nothing works if I don't return a pasteboard item? and if needed, how do you go about posting a custom object?
If somebody has an idea I would greatly appreciate it. Happy to post other code that would help with the debugging