滑动动画以删除UICollectionView中的Cell – Swift 2.0
发布时间:2020-12-14 05:25:33 所属栏目:百科 来源:网络整理
导读:我创建了一个带有UICollectionView的应用程序,如下图所示: 添加了两个手势: 第一个(向上)将擦除单元格. 第二个(向下)将更新单元格(获取CoreData的新数据). 功能很好,但没有动画. iOS有一个非常酷的动画拖动单元格,单元格消失. 我很快就是动画的初学者,所以
我创建了一个带有UICollectionView的应用程序,如下图所示:
添加了两个手势: 第一个(向上)将擦除单元格. 第二个(向下)将更新单元格(获取CoreData的新数据). 我很快就是动画的初学者,所以当它变得有点失落时. 我的问题是:如何添加占据整个单元格的动画? 我在网站上阅读了一些答案,但都是在Object-C(like this)中. 有人能帮我吗?
在UICollectionView的单元格上实现动画的最佳方法是覆盖其布局UICollectionViewLayout.它的方法将返回您想要显示/插入/删除的单元格的布局属性.
例如:我创建了一个继承UICollectionViewFlowLayout的类KDCollectionViewFlowLayout并覆盖了delete属性. class KDCollectionViewFlowLayout: UICollectionViewFlowLayout { override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let attribute = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath) attribute?.transform = CGAffineTransformTranslate(attributes.transform,ITEM_SIZE) attribute?.alpha = 0.0 return attribute } } 现在,您需要将此flowLayout的对象分配给viewDidLoad中的集合视图,或者您可以通过storyboard分配它. 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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |