【发布时间】:2015-11-25 06:04:24
【问题描述】:
我正在为 Mac (10.11) 创建一个简单的应用程序。我有 NSCollectionView,将在其上对对象进行排序。我添加了拖放支持,但 NSCollectionView 没有动画。相反,它会重新加载其内容。
func collectionView(collectionView: NSCollectionView, writeItemsAtIndexes indexes: NSIndexSet, toPasteboard pasteboard: NSPasteboard) -> Bool {
let data = NSKeyedArchiver.archivedDataWithRootObject(indexes)
pasteboard.setData(data, forType: "business_drag")
return true
}
func collectionView(collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndex proposedDropIndex: UnsafeMutablePointer<Int>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {
return NSDragOperation.Move
}
func collectionView(collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, index: Int, dropOperation: NSCollectionViewDropOperation) -> Bool {
Swift.print("acceptDrop")
let pasteboard = draggingInfo.draggingPasteboard()
let data = pasteboard.dataForType("business_drag")
let indexes = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! NSIndexSet
let draggedCell = indexes.firstIndex
let old = NSIndexPath(forItem: draggedCell, inSection: 0)
let new = NSIndexPath(forItem: index, inSection: 0)
collectionView.animator().moveItemAtIndexPath(old, toIndexPath: new)
// uncommenting this lines makes collectionView reload its conntent
// let object = collectionView.content.removeAtIndex(draggedCell)
// collectionView.content.insert(object, atIndex: index)
return true
}
我从 AppleDeveloper Portal 下载了示例代码,但它是用 Objective-C 编写的
【问题讨论】:
-
抱歉回复晚了,您指的是哪个Apple示例代码?
标签: swift macos animation drag-and-drop nscollectionview