Hide and show the carousel pager based upon the content size.

This commit is contained in:
Hedden, Kyle Matthew 2024-03-08 16:45:11 -05:00
parent aa6e55e55d
commit 2877534c70

View File

@ -55,6 +55,9 @@ open class Carousel: View {
/// The view that we use for paging
public var pagingView: (MoleculeViewProtocol & CarouselPageControlProtocol)?
/// The pagingView anchor to the bottom of the carousel. Disabled when the pagingView is hidden.
public var pagingBottomPin: NSLayoutConstraint?
/// If the carousel should loop after scrolling past the first and final cells.
public var loop = false
@ -89,10 +92,16 @@ open class Carousel: View {
// Go to current cell. layoutIfNeeded is needed otherwise cellForItem returns nil for peaking logic. The dispatch is a sad way to ensure the collection view is ready to be scrolled.
guard let model = model as? CarouselModel, !model.molecules.isEmpty,
(model.paging == true || loop == true) else { return }
DispatchQueue.main.async {
self.collectionView.scrollToItem(at: IndexPath(row: self.currentIndex, section: 0), at: self.itemAlignment, animated: false)
self.collectionView.layoutIfNeeded()
self.showPeaking(true)
DispatchQueue.main.async { [self] in
collectionView.scrollToItem(at: IndexPath(row: currentIndex, section: 0), at: itemAlignment, animated: false)
collectionView.layoutIfNeeded()
showPeaking(true)
let shouldHidePager = model.molecules.isEmpty || collectionView.contentSize.width < bounds.width
if let pagingView = pagingView, shouldHidePager != pagingView.isHidden {
pagingView.isHidden = shouldHidePager
pagingBottomPin?.isActive = !shouldHidePager
}
}
}
@ -276,7 +285,8 @@ open class Carousel: View {
addSubview(pagingView)
pagingView.centerXAnchor.constraint(equalTo: collectionView.centerXAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: pagingView.centerYAnchor, constant: position).isActive = true
bottomAnchor.constraint(greaterThanOrEqualTo: pagingView.bottomAnchor).isActive = true
pagingBottomPin = bottomAnchor.constraint(greaterThanOrEqualTo: pagingView.bottomAnchor)
pagingBottomPin?.isActive = true
bottomPin = bottomAnchor.constraint(equalTo: collectionView.bottomAnchor)
bottomPin?.priority = .defaultLow
bottomPin?.isActive = true