用于移除 UICollectionView 中的 Cell 的滑动动画 - Swift 2.0
Swipe Animation for remove Cell in UICollectionView - Swift 2.0
我用 UICollectionView
创建了一个应用,就像这张图片:
添加了两个手势:
第一个(向上)将擦除单元格。
第二个(向下)将更新单元格(获取CoreData的新数据)。
这些功能工作正常,但没有动画。 iOS 有一个非常酷的动画将单元格向上拖动然后单元格消失。
我是动画新手swift,所以有点迷茫
我的问题是:如何添加占据整个单元格的动画?
我在网站上阅读了一些答案,但都是 Object-C (like this)。
有人可以帮助我吗?
在 UICollectionView
的单元格上实现动画的最佳方法是覆盖其布局 UICollectionViewLayout
。它的方法将 return 您想要 appear/insert/delete 的单元格的布局属性。
例如:我创建了一个class KDCollectionViewFlowLayout
继承UICollectionViewFlowLayout
并覆盖删除属性。
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)
我用 UICollectionView
创建了一个应用,就像这张图片:
添加了两个手势:
第一个(向上)将擦除单元格。
第二个(向下)将更新单元格(获取CoreData的新数据)。 这些功能工作正常,但没有动画。 iOS 有一个非常酷的动画将单元格向上拖动然后单元格消失。
我是动画新手swift,所以有点迷茫
我的问题是:如何添加占据整个单元格的动画?
我在网站上阅读了一些答案,但都是 Object-C (like this)。
有人可以帮助我吗?
在 UICollectionView
的单元格上实现动画的最佳方法是覆盖其布局 UICollectionViewLayout
。它的方法将 return 您想要 appear/insert/delete 的单元格的布局属性。
例如:我创建了一个class KDCollectionViewFlowLayout
继承UICollectionViewFlowLayout
并覆盖删除属性。
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)