NSCollectionView 动画重新布局
NSCollectionView animated re-layout
我正在为 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
编写的
您需要确定两件事:
在调用 moveItemAtIndexPath
之前,请确保您没有将当前动画上下文的持续时间更改为零(默认情况下为 0.25)。您可以使用 NSAnimationContext.currentContext().duration
更改值
确保您已将 CALayer 添加到滚动视图(而非集合视图)。您可以在 Interface Builder 中打开或使用 wantsLayer
以编程方式打开
例子可以参考下面的repository,它是Apple Sample Project CocoaSlideCollection的翻译版本,它是NSCollectionView 2015的demo。检查BrowserWindow.xib
for CALayer。
https://github.com/ooper-shlab/CocoaSlideCollection-Swift
此外,我制作了视频教程,请查看“19'30”
YouTube:https://youtu.be/fEuiLhYerBA
我正在为 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
编写的您需要确定两件事:
在调用
moveItemAtIndexPath
之前,请确保您没有将当前动画上下文的持续时间更改为零(默认情况下为 0.25)。您可以使用NSAnimationContext.currentContext().duration
更改值
确保您已将 CALayer 添加到滚动视图(而非集合视图)。您可以在 Interface Builder 中打开或使用
wantsLayer
以编程方式打开
例子可以参考下面的repository,它是Apple Sample Project CocoaSlideCollection的翻译版本,它是NSCollectionView 2015的demo。检查BrowserWindow.xib
for CALayer。
https://github.com/ooper-shlab/CocoaSlideCollection-Swift
此外,我制作了视频教程,请查看“19'30”
YouTube:https://youtu.be/fEuiLhYerBA