Oliver's answer worked for me perfectly, but it had some unnecessary iterations. Therefore, I created a much simpler implementation based on it:
override public func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint,
withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView else {
return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
}
/// the actual page width without any insets or margins
let pageContentWidth = self.itemSize.width
/// total page width
let pageWidth = pageContentWidth + self.minimumLineSpacing
/// current page estimate based on the proposed contentoffset
let proposedPage = round(proposedContentOffset.x / pageWidth)
/// getting the proposed page content offset (the start position of the page we are supposed to take rest at)
let proposedXContentOffsetBasedOnProposedPage = proposedPage * pageWidth
/// minimum allowed content offset
let minContentOffset: CGFloat = 0
/// maximum allowed content offset
let maxContentOffset = collectionViewContentSize.width-pageContentWidth
let bestCandidateOffset: CGFloat
/// if the velocity is positive ( the user is scrolling to the right)
if velocity.x > 0 {
/// the scroll final position should always advance to the RIGHT; hence if the `proposedXContentOffsetBasedOnProposedPage` is LESS than the original proposed offset this means that the estimated page was estimated wrong and it will go back. However, because the velocity is to right this means that we need to go to the NEXT page directly hence (proposedPage+1)
let nearestCandidateOffsetToRight = proposedXContentOffsetBasedOnProposedPage < proposedContentOffset.x ?
(proposedPage+1)*pageWidth :
proposedXContentOffsetBasedOnProposedPage
/// making sure not to go out of contentSize bounds
bestCandidateOffset = min(maxContentOffset, nearestCandidateOffsetToRight)
} else if velocity.x < 0 { /// if the velocity is negative ( the user is scrolling to the left)
/// the scroll final position should always advance to the LEFT; hence if the `proposedXContentOffsetBasedOnProposedPage` is GREATER than the original proposed offset this means that the estimated page was estimated wrong and it will move forward. However, because the velocity is to left this means that we need to go to the PREVIOUS page directly hence (proposedPage-1)
let nearestCandidateOffsetToLeft = proposedXContentOffsetBasedOnProposedPage > proposedContentOffset.x ?
(proposedPage-1)*pageWidth :
proposedXContentOffsetBasedOnProposedPage
/// making sure not to go out of contentSize bounds
bestCandidateOffset = max(minContentOffset, nearestCandidateOffsetToLeft)
} else { /// if the velocity is 0
/// move directly to the `proposedXContentOffsetBasedOnProposedPage`
bestCandidateOffset = proposedXContentOffsetBasedOnProposedPage
}
return CGPoint(x: bestCandidateOffset, y: proposedContentOffset.y)
}
If you have tried this solution and the others mentioned here, you may have noticed that scrolling too fast could cause skipping multiple pages, which is not the case when isPagingEnabled is set to true.
This implementation only ensures that the UICollectionView's final resting position is always aligned with a page. It does not take into account the limitation of scrolling to one page at a time.
To limit scrolling to one page at a time, regardless of velocity, you can implement the scrollViewWillBeginDragging method of the UIScrollView and save the page from which the user started scrolling. Then, in the scrollViewWillEndDragging method, you can determine the final resting position based on the starting page.
/// used to save the page the user started the scroll from
var startScrollPage: Int?
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
/// the actual page width without any insets or margins
let pageContentWidth = collectionView.collectionViewLayout.itemSize.width
/// total page width
let pageWidth = pageContentWidth + collectionView.collectionViewLayout.minimumLineSpacing
/// current page estimate based on the proposed contentoffset
let currentPage = round(scrollView.contentOffset.x / pageWidth)
startScrollPage = Int(currentPage)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView,
withVelocity velocity: CGPoint,
targetContentOffset: UnsafeMutablePointer<CGPoint>) {
guard let startScrollPage = startScrollPage else { return }
/// the actual page width without any insets or margins
let pageContentWidth = collectionView.collectionViewLayout.itemSize.width
/// total page width
let pageWidth = pageContentWidth + collectionView.collectionViewLayout.minimumLineSpacing
/// current page estimate based on the proposed contentoffset
let proposedPage = round(targetContentOffset.pointee.x / pageWidth)
/// getting the proposed page content offset (the start position of the page we are supposed to take rest at)
let proposedXContentOffsetBasedOnProposedPage = proposedPage * pageWidth
/// minimum allowed content offset
let minContentOffset: CGFloat = 0
/// maximum allowed content offset
let maxContentOffset = scrollView.contentSize.width-pageContentWidth
let bestCandidateOffset: CGFloat
/// if the velocity is positive ( the user is scrolling to the right)
if velocity.x > 0 {
/// get the next page offset by adding 1 to the starting page
let nearestCandidateOffsetToRight = CGFloat(startScrollPage+1)*pageWidth
/// making sure not to go out of contentSize bounds
bestCandidateOffset = min(maxContentOffset, nearestCandidateOffsetToRight)
} else if velocity.x < 0 { /// if the velocity is negative ( the user is scrolling to the left)
/// get the prev page offset by subtracting 1 from the starting page
let nearestCandidateOffsetToLeft = CGFloat(startScrollPage-1)*pageWidth
/// making sure not to go out of contentSize bounds
bestCandidateOffset = max(minContentOffset, nearestCandidateOffsetToLeft)
} else { /// if the velocity is 0
/// move directly to the `proposedXContentOffsetBasedOnProposedPage`
bestCandidateOffset = proposedXContentOffsetBasedOnProposedPage
}
targetContentOffset.pointee.x = bestCandidateOffset
self.startScrollPage = nil
}