This is a pretty straightforward snapping logic for a collection view with cells of same size, and one section (logic for more sections can be easily added).
scrollViewWillEndDragging has an
inout targetContentOffset parameter, so we can read, calculate and modify the end position of the scroll. Luckily, we don't need to take into consideration insets, line or item spacing (lost a lot of time including them, not understanding why the correct math produces wrong results), but we do need to consider the case where the user scrolls past the last cell - the
targetContentOffset will be within bounds, but the […]
Continue reading →