2

I have created a UICollectionView that slides its items based on a certain trigger. I'm trying to animate the cells transition by:

            UIView.animate(withDuration: 0.4,
                           delay: 0.5, animations: {

                            self.headingsCollectionView.contentOffset = CGPoint(x: x, y: 0)
                            self.headingsCollectionView.reloadItems(at: [indexPath])
            })

The animation is successful, the problem is when I change the CollectionView contentOffset the current item disappears instead of scrolling off.

  • I tried reloading the current item (as suggested here) so it would stay visible but it's still not working.
  • I've tried scrollToItemAtIndexPath:atScrollPosition:animated but it's not animating the item transition.
  • setContentOffset:animated Tried it, it doesn't animate as well.
Dilip Tiwari
  • 1,441
  • 18
  • 31
Shawerma
  • 171
  • 1
  • 4
  • 12

1 Answers1

4

Because you are using UIView.animate method, you have to put self.view.layoutIfNeeded() after programmatically setting contentOffset

 UIView.animate(withDuration: 0.4,
                   delay: 0.5, animations: {

                    self.headingsCollectionView.contentOffset = CGPoint(x: x, y: 0)
                    self.headingsCollectionView.reloadItems(at: [indexPath])
                    self.view.layoutIfNeeded()
    })