Home / IOS Development / ios – Incomplete delete animation for a single row of horizontal UICollectionView with UICollectionViewCompositionalLayout

ios – Incomplete delete animation for a single row of horizontal UICollectionView with UICollectionViewCompositionalLayout



At the moment I have a single row horizontal UICollectionView with UICollectionViewCompositionalLayout

[All] [Calendar] [Home2] [Work3] [Work4] ... [Work8] [⚙]

The UICollectionView, acts as a scrollable tab layout navigation bar.

When the user switches to [⚙] since, he is able to delete the tab in front of himself. In this case, the category during the first time of deletion [Work8] will be removed.

Here is the simple code to achieve it.


https://github.com/yccheok/ios-tutorial/blob/debug/TabDemo/TabDemo/ViewController.swift#L1

44

func debug() {
    if tabInfos.count < 2 {
        return
    }
    
    let index = tabInfos.count-2
    tabInfos.remove(at: index)
    let indexPath = getIndexPath(index)
    self.tabCollectionView.deleteItems(at: [indexPath])
    
    // TODO: Possible solution.
    /*
    self.tabCollectionView.reloadData()
    DispatchQueue.main.async() {
        let indexPath = self.getIndexPath(self.tabInfos.count-1)
        self.tabCollectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically)
        self.tabCollectionView.scrollToItem(at: indexPath, at: .right, animated: true)
    }
     */

    // Clear left/ right cached view controllers - https://stackoverflow.com/a/21624169/72437
    pageViewController.dataSource = nil
    pageViewController.dataSource = self

    // Don't forget to adjust the selection index.
    if index < self.selectedTabInfoIndex {
        selectedTabInfoIndex -= 1
    }
}

Strange outcome

https://www.youtube.com/watch?v=Wzgb4QErUes (Watch this video for complete deletion animation)

This is what it looks like before deletion

enter image description here

As you can see in the video, after deleting [Work8]left tab ([Work3]) is not visible

enter image description here

My expected outcome is, [Work3] should be visible to immediately, without further action by the user. I have to press UICollectionView, move it a little, only all the tabs are displayed

enter image description here


I do not prefer to use reloadData, as

  1. It will not preserve the scroll position to UICollectionView.
  2. It has no animation.

Do you have any idea what is the reason for this strange animation result?

Here is the complete source code to try it out - https://github.com/yccheok/ios-tutorial/releases/tag/debug


Source link