如何通过在UICollectionView中插入新单元来获取动画

时间:2016-05-18 15:53:40

标签: ios swift animation uicollectionview

我想将一个collectionview单元格添加到带动画的UICollectionView中。不幸的是,我没有看到任何动画。

这就是我试过的

let newIndexPath = NSIndexPath(forRow: row, inSection: 0)

首先滚动到单元格

self.collectionView.scrollToItemAtIndexPath(NSIndexPath(NSIndexPath(forRow: row-1, inSection: 0), atScrollPosition: .Top, animated: true)
self.collectionView.insertItemsAtIndexPaths([newIndexPath])

// => no animation

插入

后选择带动画的项目
self.collectionView.insertItemsAtIndexPaths([newIndexPath])
self.collectionView.selectItemAtIndexPath(newIndexPath, inSection: 0), animated: true, scrollPosition: .Top)

// => no animation

手动调用didSelectItemAtIndexPath(不是很优雅而不是首选方法)

self.collectionView.selectItemAtIndexPath(newIndexPath, inSection: 0), animated: true, scrollPosition: .Top)
self.collectionView(self.collectionView, didSelectItemAtIndexPath: newIndexPath)

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
          UIView.animateWithDuration(1.0, animations: {
        self.collectionView.cellForItemAtIndexPath(indexPath)?.alpha = 0.5
    })

    UIView.animateWithDuration(1.0, animations: {
        self.collectionView.cellForItemAtIndexPath(indexPath)?.alpha = 1
    })
}

// => no animation

但是,当我用

删除一个单元格时
self.collectionView.deleteItemsAtIndexPaths([newIndexPath])

我确实看过动画。我做错了什么,插入单元格时如何显示默认动画?

N.B。 self.collectionView.allowsSelection = true

0 个答案:

没有答案