Last active
January 31, 2018 12:58
-
-
Save Nautiyalsachin/c4af8450998dae6282b056a13dd3d57b to your computer and use it in GitHub Desktop.
Used for providing custom paging for collection view.
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: 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