Skip to content

Commit

Permalink
πŸ› Ensure the same carousel step is always shown on orientation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed Jun 24, 2022
1 parent f185294 commit ed54a8c
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ extension AppcuesCarouselTrait {
preferredContentSize = container.preferredContentSize
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
// Use the currentPage value instead of collectionView.indexPathsForVisibleItems because
// that occasionally includes additional items.
let targetIndex = IndexPath(item: pageMonitor.currentPage, section: 0)

// Using `coordinator.animate` would be ideal, and it does work,
// but that animation is jankier when going from landscape to portrait.
DispatchQueue.main.async {
self.carouselView.collectionView.scrollToItem(at: targetIndex, at: .centeredHorizontally, animated: false)
}

super.viewWillTransition(to: size, with: coordinator)
}

func navigate(to pageIndex: Int, animated: Bool) {
carouselView.collectionView.scrollToItem(
at: IndexPath(row: pageIndex, section: 0),
Expand Down

0 comments on commit ed54a8c

Please sign in to comment.