Created
August 17, 2016 14:26
-
-
Save harryworld/cf362f848f643c1f1127773be1c12623 to your computer and use it in GitHub Desktop.
Animate NSCollectionViewItem during dragging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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