Skip to content

Instantly share code, notes, and snippets.

@harryworld
Created August 17, 2016 14:26
Show Gist options
  • Save harryworld/cf362f848f643c1f1127773be1c12623 to your computer and use it in GitHub Desktop.
Save harryworld/cf362f848f643c1f1127773be1c12623 to your computer and use it in GitHub Desktop.
Animate NSCollectionViewItem during dragging
func collectionView(collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAtPoint screenPoint: NSPoint, forItemsAtIndexPaths indexPaths: Set<NSIndexPath>) {
if let indexPath = draggingIndexPaths.first,
item = collectionView.itemAtIndexPath(indexPath) {
draggingItem = item // draggingItem is temporary instance variable of NSCollectionViewItem
}
}
func collectionView(collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAtPoint screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
draggingItem = nil
}
func collectionView(collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath?>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {
if let proposedDropIndexPath = proposedDropIndexPath.memory,
draggingItem = draggingItem,
currentIndexPath = collectionView.indexPathForItem(draggingItem)
where currentIndexPath != proposedDropIndexPath { // guard to move once only
collectionView.animator().moveItemAtIndexPath(currentIndexPath, toIndexPath: proposedDropIndexPath)
}
return .Move
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment