Created
March 10, 2016 03:00
-
-
Save benjaminsnorris/a19cb14082b28027d183 to your computer and use it in GitHub Desktop.
Snap to center collection view cell
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
class CollectionViewController: UIViewController { | |
... | |
func snapToCenter() { | |
let centerPoint = view.convertPoint(view.center, toView: collectionView) | |
guard let centerIndexPath = collectionView.indexPathForItemAtPoint(centerPoint) | |
collectionView.scrollToItemAtIndexPath(centerIndexPath, atScrollPosition: .CenteredHorizontally, animated: true) | |
} | |
... | |
} | |
// MARK: - Scroll view delegate | |
extension CollectionViewController: UIScrollViewDelegate { | |
func scrollViewDidEndDecelerating(scrollView: UIScrollView) { | |
snapToCenter() | |
} | |
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { | |
if !decelerate { | |
snapToCenter() | |
} | |
} | |
} |
Hey dude, very great job, you simplify a lot of work. Congratulations! 🎉 🥂 🙌
Awesome !!! It's the simplest and great solution I've ever found before to handle snapping scroll view on UIScrollView
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man, I wanna say that this is the cleanest solution I got for this problem and working so nicely.
have a good day :) 🥇