I have a requirement to animate UICollectionView and UICollectionViewCell frame. The end results is what I but in between there appear to be some phantom cells being mixed in with actual cells being animated. The CV animates to a different size and the CV cells are to match the new size based on settings given to CV's collectionViewLayout.
Here's the starting point:
Here's how the animation starts to look:
The end result is as expected:
My final intent is to do this with a UIViewPropertyAnimator() but you see the same problem even if the animation is triggered with a standard UIView animation.
The main method called is prepareLayout().
internalfunc prepareLayout() {
let spacing = buttonTheme.spacing * 2.0
// let theLayout = UICollectionViewFlowLayout()
theLayout.invalidateLayout()
buttonsView.frame = viewModel.frame
theLayout.itemSize = itemSize
theLayout.minimumInteritemSpacing = spacing
theLayout.minimumLineSpacing = spacing
// Needed to correct problem where sometimes only one row (instead of two) shows in iOS 10.x
buttonsView.contentInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
// buttonsView.performBatchUpdates({
buttonsView.setCollectionViewLayout(theLayout, animated: true)
self.buttonsView.reloadData()
// }, completion: nil)
buttonsView.setCollectionViewLayout(theLayout, animated: true)
}
As can seen, I've been playing around with CV's performBatchUpdates() and setCollectionViewLayout() but to no avail. I need to reloadData() because things like the fontSize change during the animation which will be more important when using UIViewPropertyAnimator().
My cell is a subclass of UICollectionViewCell that has a UIButton in it. The interaction of the button is disabled so I can handle everything in the CV.
Any idea on how to correct this problem is appreciated.