Animate to delete a cell in UICollectionView - Swift 2.0

I created an application with a UICollectionView as this image:

enter image description here

Two gestures added:

The first (up) erases the cell.

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

I'm new to animation fast, so I lost it a bit.

My question is: how to add an animation that takes up the whole cell?

I read a few answers on the site, but everything is in Object-C ( like this ).

Can someone help me?

+5
source share
1 answer

The best way to achieve animation in a UICollectionView cell is to override its UICollectionViewLayout layout. It has a method that will return the cell layout attributes that you want to either display / insert / delete.

For example: I created the KDCollectionViewFlowLayout class KDCollectionViewFlowLayout inherit from UICollectionViewFlowLayout and overriding 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 } } 

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

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

You are now set to convert the cell that you defined to the finalLayoutAttributesForDisappearingItemAtIndexPath method whenever you perform a delete operation on collectionView .

Update

You need to remove items from the collection using a 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) 
+13
source

Source: https://habr.com/ru/post/1234341/


All Articles