Search code examples
iosanimationuicollectionviewrotationuicollectionviewlayout

How to remove fade animation of UICollectionView during rotation time?


I am just trying to adapt animation of CollectionView during rotation time , so that there wouldn't be any fade effects like this

one

Just simple move of the view. So after a small search i found out that I accomplish that effect by just overriding two methods of UICollectionViewFlowLayout

 override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

return nil

}
 override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
      return layoutAttributesForItem(at: itemIndexPath) 

}

But then another issue came into effect: when i receive message in between two messages like receiving messages with delay

two

Blue messages not just shift down, but old instance of a blue message is stays in place for short time and then goes down. But if I return to initial prefernces both

  override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

return nil

}
 override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
      return nil 

}

Messages moves appropriately in a neat way but fading effect during rotation again takes place.

This thing is very confused me, please maybe someone has any clue how can I keep rotation animation without fading effect and make messages animation effect like a plain messages movement.


Solution

  • I think I found the solution. 1) I need to find out the IndexPaths of elements being inserted in CollectionView. 2) Calculate next elements laying after IndexPath being inserted(when i receive message in between two messages like receiving messages with delay ) 3) applying new initialLayoutAttributesForAppearingItem and finalLayoutAttributesForDisappearingItem to my IndexPaths array derived above, for the rest elements we just applying that preferences

     override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    
    return nil
    
    }
        override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
      return layoutAttributesForItem(at: itemIndexPath) 
    
     }
    

    here is code

       override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
    
        super.prepare(forCollectionViewUpdates: updateItems)
                   insertingIndexPaths.removeAll()
        print("begining : \(insertingIndexPaths)")
    
    
        // create an array
    
        let fullAmountOfCells = collectionView?.numberOfItems(inSection: 0)
        print("number of items: \(fullAmountOfCells)")
    
        for update in updateItems {
            if let indexPath = update.indexPathAfterUpdate,
                update.updateAction == .insert {
                insertingIndexPaths.append(indexPath)
                print("Example if indexPath if for loop:\(indexPath)")
            }
    
       }
        let lastPathOfInsertingElement = insertingIndexPaths.last
        let differenceBetweenFullAmountAndLastInsertElement = fullAmountOfCells! - (lastPathOfInsertingElement?.item)! - 1
    
        if differenceBetweenFullAmountAndLastInsertElement > 0 {
            for numeric in 1...differenceBetweenFullAmountAndLastInsertElement {
                insertingIndexPaths.append(IndexPath(item: numeric + (lastPathOfInsertingElement?.item)!, section: 0))
            }
            print("True array to be modified with attributes:\(insertingIndexPaths)")
    
    }
     }
    
    override func finalizeCollectionViewUpdates() {
        super.finalizeCollectionViewUpdates()
      //  ChatLogController.orientation = UIDevice.current.orientation
    //    print(  ChatLogController.orientation = UIDevice.current.orientation)
        insertingIndexPaths.removeAll()
        movingIndexPath.removeAll()
    }
    
    
    
       override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attributes = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
    
        if insertingIndexPaths.contains(itemIndexPath) {
            // attributes?.alpha = 0.0
            //attributes?.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
    
            print("Process in initialLayoutAttributesForAppearingItem: \(itemIndexPath)")
               return attributes
    
        } else {
             print("Process in initialLayout set to nil: \(itemIndexPath)")
            return nil
        }
    
    
    }
    
    
    
    
    
    
    
    
      override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
    
    if insertingIndexPaths.contains(itemIndexPath) {
        // attributes?.alpha = 0.0
        //attributes?.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
    
    
        return nil
    
    } else {
        print("processing final layout and it to leyoutAttributeForItem(at: \(itemIndexPath)")
        return layoutAttributesForItem(at:itemIndexPath)
    }
    
    
    
      }