【问题标题】:Swipe Animation for remove Cell in UICollectionView - Swift 2.0滑动动画以删除 UICollectionView 中的单元格 - Swift 2.0
【发布时间】:2015-10-23 04:24:51
【问题描述】:

我使用UICollectionView 创建了一个应用程序,如下图所示:

添加了两个手势:

第一个(向上)将擦除单元格。

第二个(向下)将更新单元格(获取CoreData的新数据)。 这些功能工作正常,但没有动画。 iOS 有一个非常酷的动画,将单元格向上拖动,然后单元格消失。

我是 swift 动画的初学者,所以我有点迷茫。

我的问题是:如何添加占据整个单元格的动画?

我在网站上阅读了一些答案,但都在 Object-C (like this) 中。

有人可以帮我吗?

【问题讨论】:

  • James 你的应用看起来很棒!
  • 詹姆斯,你有没有按照你的意愿让它工作?希望在 Swift 中实现完全相同类型的功能。很想看看你的应用,如果也能工作的话!

标签: swift animation uicollectionview uicollectionviewcell


【解决方案1】:

UICollectionView 的单元格上实现动画的最佳方法是覆盖其布局UICollectionViewLayout。它的 has 方法将返回您想要显示/插入/删除的单元格的布局属性。

例如:我创建了一个类KDCollectionViewFlowLayout继承UICollectionViewFlowLayout并覆盖了delete属性。

class KDCollectionViewFlowLayout: UICollectionViewFlowLayout {

  override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let attribute = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath)

        attribute?.transform = CGAffineTransformTranslate(attributes.transform, 0, ITEM_SIZE)
        attribute?.alpha = 0.0
        return attribute

    }
}

现在您需要将此 flowLayout 的对象分配给 viewDidLoad 中的集合视图,或者您可以通过情节提要分配它。

let flowLayout = KDCollectionViewFlowLayout()
self.collectionView?.setCollectionViewLayout(flowLayout, animated: true)

现在,只要您对collectionView 执行任何删除操作,您就可以将您定义的单元格转换为finalLayoutAttributesForDisappearingItemAtIndexPath 方法。

更新

您需要使用批处理操作从集合视图中删除项目。

collectionView.performBatchUpdates({ () -> Void in
   //Array of the data which you need to deleted from collection view
    let indexPaths = [NSIndexPath]()
    //Delete those entery from the data base. 

    //TODO: Delete the information from database

    //Now Delete those row from collection View 

    collectionView.deleteItemsAtIndexPaths(indexPaths)

}, completion:nil)

【讨论】:

  • 对,首先,我试图适应我的代码,但没有成功。我的代码分为三个:一个类和两个扩展。 link我如何在我的代码中加入他的例子?我比最初更困惑......
  • 对不起,没在这里工作,你可以发一个链接来下载你的例子,让我看看你是如何组织一切的?
  • @James,完美的sample code 删除动画。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多