What I'm trying to do:
In the Apple Photo App, if you rotate the device while scrolling to an arbitrary offset, the same cell that was in the center in advance would get to the center after the rotation.
I am trying to achieve the same behavior with a UICollectionView. ' indexPathsForVisibleItems ' seems like a way to do this ....
What I got so far:
The following code provides smooth operation between rotations, but the calculation of the central element seems to be off:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
self.collectionView?.collectionViewLayout.invalidateLayout()
let middleItem = (self.collectionView?.indexPathsForVisibleItems.count / 2) - 1
let indexPath = self.collectionView?.indexPathsForVisibleItems[middleItem]
coordinator.animate(alongsideTransition: { ctx in
self.collectionView?.layoutIfNeeded()
self.collectionView?.scrollToItem(at: indexPath!, at: .centeredVertically, animated: false)
}, completion: { _ in
})
}
What does not work with the above code:
- Portrait → Landscape : a little from what cell should be in the center.
- → → : , .
- → → → : !
:
- -
.
- self.collectionView?.layoutIfNeeded()
, - scrollToItem
, ?
?
indexPathsForVisibleItems ' " contentOffset"? ? , contentSize , ..?