使用NSCollectionViewFlowLayout在NSCollectionView上没有动画

时间:2018-03-22 11:00:34

标签: swift cocoa nscollectionview

我正在以编程方式设置一个简单的集合视图,但没有看到我希望免费获得的插入/删除动画。我已经检查了与集合视图相关的类的所有方法,并且无法找到任何似乎影响动画的内容。请问我在这个设置中缺少什么?

在索引0处插入内容时,我希望看到现有项目移动并且新项目淡入。

class ECTestViewController: NSViewController
{
    var scrollView: NSScrollView!
    var collectionView: NSCollectionView!

    var items = [Int]()

    override func loadView() {
        view = NSView()
        view.translatesAutoresizingMaskIntoConstraints = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        scrollView = NSScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(scrollView)
        let scrollViewList = ["view": scrollView]
        var scrollViewConstraints = NSLayoutConstraint.constraints(withVisualFormat: "|[view]|", metrics: nil, views: scrollViewList)
        scrollViewConstraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-22-[view]-8-|", metrics: nil, views: scrollViewList)
        NSLayoutConstraint.activate(scrollViewConstraints)

        collectionView = NSCollectionView()
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.dataSource = self
        collectionView.delegate = self
        scrollView.documentView = collectionView

        collectionView.collectionViewLayout = NSCollectionViewFlowLayout()

        // Register the item types for the collection
        collectionView.register(ECTestItem.self, forItemWithIdentifier: ECTestItem.interfaceIdentifier)

        // Add our first item
        items.append(0)

        // Buttons to add and remove items
        let openImage = NSImage(named: NSImage.Name("Open"))
        let openButton = ECImageButton(image: openImage!)
        view.addSubview(openButton)
        openButton.action = #selector(openItem)
        openButton.target = self

        let closeImage = NSImage(named: NSImage.Name("Close"))
        let closeButton = ECImageButton(image: closeImage!)
        view.addSubview(closeButton)
        closeButton.action = #selector(closeItem)
        closeButton.target = self
    }

    @objc func closeItem() {
        items.remove(at: 0)
        collectionView.deleteItems(at: [IndexPath(item: 0, section: 0)])
    }

    @objc func openItem() {
        items.insert(0, at: 0)
        collectionView.insertItems(at: [IndexPath(item: 0, section: 0)])
    }
}

extension ECTestViewController: NSCollectionViewDataSource
{
    func numberOfSections(in collectionView: NSCollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        return collectionView.makeItem(withIdentifier: ECTestItem.interfaceIdentifier, for: indexPath)
    }
}

extension ECTestViewController: NSCollectionViewDelegateFlowLayout // Actually set as the collection view delegate
{
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
        return NSMakeSize(100, 100)
    }
}

class ECTestItem: NSCollectionViewItem
{
    static var interfaceIdentifier: NSUserInterfaceItemIdentifier { get { return NSUserInterfaceItemIdentifier("ouliner") } }

    override func loadView() {
        view = NSView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.wantsLayer = true
        view.layer?.backgroundColor = CGColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

1 个答案:

答案 0 :(得分:3)

要进行动画更改,需要通过动画制作代理调用方法。在下面进行更改给出了我期望的动画。

不正确:

collectionView.insertItems(at: [IndexPath(item: 0, section: 0)])

正确:

collectionView.animator().insertItems(at: [IndexPath(item: 0, section: 0)])

这不是我之前所知的概念,并且在集合视图动画文档中没有提及它。