首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSCollectionView动画重新布局

NSCollectionView动画重新布局
EN

Stack Overflow用户
提问于 2015-11-25 06:04:24
回答 1查看 2.5K关注 0票数 1

我正在为Mac (10.11)创建一个简单的应用程序。我有NSCollectionView,它将对对象进行排序。我增加了拖放支持,但是NSCollectionView没有动画。相反,它重新加载其内容。

代码语言:javascript
复制
    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门户下载了示例代码,但它是用Objective编写的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-27 15:22:28

您需要确保的两件事:

  1. 在调用moveItemAtIndexPath之前,请确保没有将当前动画上下文的持续时间更改为零(默认为0.25)。可以使用NSAnimationContext.currentContext().duration更改值。
  2. 确保将CALayer添加到滚动视图(而不是集合视图)。您可以在Interface中打开或以编程方式使用wantsLayer

一个例子可以参考下面的存储库,它是苹果示例项目CocoaSlideCollection的翻译版本,它是NSCollectionView 2015的演示程序。检查BrowserWindow.xib中的CALayer。

https://github.com/ooper-shlab/CocoaSlideCollection-Swift

此外,我创建了一个视频教程,检查19'30“在这上面

Youtube:https://youtu.be/fEuiLhYerBA

源代码:https://github.com/harryworld/NSCollectionView-DragDrop

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33909502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档