Skip to content

Instantly share code, notes, and snippets.

@celian-m
Created March 21, 2019 09:21
Show Gist options
  • Save celian-m/8c68b779e2c361f8bfe8ddeadaf04099 to your computer and use it in GitHub Desktop.
Save celian-m/8c68b779e2c361f8bfe8ddeadaf04099 to your computer and use it in GitHub Desktop.
Add custom page size to UIScrollview
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Page width used for estimating and calculating paging.
let pageWidth = (self.frame.width) - interIntemMargin
// Make an estimation of the current page position.
let approximatePage = scrollView.contentOffset.x/pageWidth
// Determine the current page based on velocity.
let currentPage = (velocity.x < 0.0) ? floor(approximatePage) : ceil(approximatePage)
// Create custom flickVelocity.
let flickVelocity = velocity.x * 0.3
// Check how many pages the user flicked, if <= 1 then flickedPages should return 0.
let flickedPages = (abs(round(flickVelocity)) <= 1) ? 0 : round(flickVelocity)
// Calculate newHorizontalOffset.
let newHorizontalOffset = ((currentPage + flickedPages ) * pageWidth) - scrollView.contentInset.left
targetContentOffset.pointee.x = newHorizontalOffset
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment