Skip to content

Instantly share code, notes, and snippets.

@Nautiyalsachin
Last active January 31, 2018 12:58
Show Gist options
  • Save Nautiyalsachin/c4af8450998dae6282b056a13dd3d57b to your computer and use it in GitHub Desktop.
Save Nautiyalsachin/c4af8450998dae6282b056a13dd3d57b to your computer and use it in GitHub Desktop.
Used for providing custom paging for collection view.
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageWidth: Float = 214+22 // PagingWidth = CellWidth + Cell Space
let currentOffset: Float = Float(scrollView.contentOffset.x)
let targetOffset: Float = Float(targetContentOffset.pointee.x)
var newTargetOffset: Float = 0
if targetOffset > currentOffset {
newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth
}
else {
newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth
}
if newTargetOffset < 0 {
newTargetOffset = 0
}
else if (newTargetOffset > Float(scrollView.contentSize.width)){
newTargetOffset = Float(Float(scrollView.contentSize.width))
}
targetContentOffset.pointee.x = CGFloat(currentOffset)
scrollView.setContentOffset(CGPoint(x: CGFloat(newTargetOffset), y: scrollView.contentOffset.y), animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment