There is a common way of handling scrolling with UICollectionView
so that when the user stops scrolling it gradually slows down and stops with the nearest item in the collection view lined up with the edge of the collection view's bounds.
This differs from page views in that the user can quickly scroll past several items in the connection view with a single swipe, and multiple items are displayed onscreen at once.
You can see this in Apple's own apps: TV, Music, Books, and Podcasts all exhibit this behavior when browsing horizontal lists in each respective app.
With UICollectionView
(or UIScrollView
, but it's more typical in collection views) this is typically implemented using the scrollViewWillEndDragging(scrollView:velocity:targetContentOffset:)
method to detect when the user has stopped scrolling and supplying it with an updated target offset to scroll to.
The top answer (by rob mayoff) to the following question describes this in some detail:
How to snap horizontal paging to multi-row collection view like App Store?
My question: How do I do this using SwiftUI's ScrollView?
I have a horizontal ScrollView enclosing an HGrid with a set of items in it. I would like to implement a horizontal browser similar to that in Apple's apps. I can do it using a UICollectionView, as described above, but would much prefer to do it "natively" in SwiftUI without wrapping a UICollectionView.
I would expect this to be possible using ScrollViewReader and scrollTo(_:anchor:)
somehow, but it doesn't seem that it has anything to addresses this need.
Is this behavior possible to implement natively using SwiftUI, and - if so - how?