Created
September 6, 2018 08:05
-
-
Save dsk1306/b31650c47aa4a41e39b25a0b6c67ee2f to your computer and use it in GitHub Desktop.
Custom UIScrollView paging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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