Skip to content

Instantly share code, notes, and snippets.

@dsk1306
Created September 6, 2018 08:05
Show Gist options
  • Save dsk1306/b31650c47aa4a41e39b25a0b6c67ee2f to your computer and use it in GitHub Desktop.
Save dsk1306/b31650c47aa4a41e39b25a0b6c67ee2f to your computer and use it in GitHub Desktop.
Custom UIScrollView paging
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageWidth = <#page width#>
let collectionViewContentWidth = collectionView.contentSize.width
if velocity.x == 0 {
page = Int(floor((targetContentOffset.pointee.x - pageWidth / 2) / pageWidth) + 1.0)
} else {
page = velocity.x > 0 ? page + 1 : page - 1
if page < 0 {
page = 0
}
let pagesNumber = collectionViewContentWidth / pageWidth
if page + 1 > Int(pagesNumber) {
page = Int(ceil(pagesNumber) - 1.0)
}
}
targetContentOffset.pointee = CGPoint(x: CGFloat(page) * pageWidth, y: targetContentOffset.pointee.y)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment