2021年3月19日星期五

Pull to refresh for UICollectionView in ViewController jumps when applying NSDiffableDataSourceSnapshot

I've added a UICollectionView as a subview to my ViewController. I've also set a UIRefreshControl for the UICollectionView which applies a NSDiffableDataSourceSnapshot when I pull down to refresh. The animation jumps when applying the snapshot (note this happens even when the navigator bar title isn't large.)

enter image description here

Here are the relevant snippets of code

var collectionView: UICollectionView! = nil  var dataSource: UICollectionViewDiffableDataSource<Section, Item>! = nil    override func viewDidLoad() {      super.viewDidLoad()            let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout:       generateLayout())      view.addSubview(collectionView)      collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]      collectionView.backgroundColor = .systemGroupedBackground      self.collectionView = collectionView      collectionView.delegate = self        let refreshControl = UIRefreshControl()      refreshControl.addTarget(self, action: #selector(doSomething), for: .valueChanged)      collectionView.refreshControl = refreshControl  }        @objc func doSomething(refreshControl: UIRefreshControl) {      DispatchQueue.main.async {          var dataSourceSnapshot = NSDiffableDataSourceSnapshot<Section, Item>()          // add data          dataSource.apply(dataSourceSnapshot)      }      refreshControl.endRefreshing()  }  

Are there any changes I can make to apply the snapshot without the sudden jump?

https://stackoverflow.com/questions/66626029/pull-to-refresh-for-uicollectionview-in-viewcontroller-jumps-when-applying-nsdif March 14, 2021 at 11:04PM

没有评论:

发表评论