Swipe Animation for remove Cell in UICollectionView - Swift 2.0

后端 未结 1 590
南笙
南笙 2020-12-28 10:06

I created an app with UICollectionView like this image:

Added two gestures:

The first (up) will erase the cell.

The second (dow

相关标签:
1条回答
  • 2020-12-28 10:42

    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.

    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
    
        }
    }
    

    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)
    

    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.

    Update

    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)
    
    0 讨论(0)
提交回复
热议问题