滑动动画以删除 UICollectionView 中的单元格 - Swift 2.0 [英] Swipe Animation for remove Cell in UICollectionView - Swift 2.0

查看:15
本文介绍了滑动动画以删除 UICollectionView 中的单元格 - Swift 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created an app with UICollectionView like this image:

添加了两个手势:

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

The first (up) will erase the cell.

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

The second (down) will update the cell (take new data of CoreData). The functions work fine, but there's no animation. iOS has a very cool animation dragging the cell up and the cell disappears.

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

I am a beginner in animations swift, so I'm a little lost when it.

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

My question is: How can I add an animation that takes up the entire cell?

我在网站上阅读了一些答案,但都在 Object-C 中(像这样).

I read some answers on the site, but all in Object-C (like this).

有人可以帮我吗?

推荐答案

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

The best way to achieve the animation on the cell of UICollectionView is overriding its layout UICollectionViewLayout. Its has method which will return the layout attributes of the cell which you want to either appear/insert/delete.

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

For example: I created a class KDCollectionViewFlowLayout inheriting UICollectionViewFlowLayout and override the delete attribute.

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 中的集合视图,或者您可以通过情节提要分配它.

Now you need to assign object of this flowLayout to the collection view in either viewDidLoad or you can assign it through storyboard.

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

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

Now, you are all set for transformation of cell which you defined in to finalLayoutAttributesForDisappearingItemAtIndexPath method whenever you perform any delete operation on the collectionView.

更新

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

You need delete the items from collection view using batch operation.

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)

这篇关于滑动动画以删除 UICollectionView 中的单元格 - Swift 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆