How to programmatically scroll through a collection view?
Swift 5
Based on Mr.Bean's answer, here is an elegant way using UICollectionView extension:
extension UICollectionView {
func scrollToNextItem() {
let contentOffset = CGFloat(floor(self.contentOffset.x + self.bounds.size.width))
self.moveToFrame(contentOffset: contentOffset)
}
func scrollToPreviousItem() {
let contentOffset = CGFloat(floor(self.contentOffset.x - self.bounds.size.width))
self.moveToFrame(contentOffset: contentOffset)
}
func moveToFrame(contentOffset : CGFloat) {
self.setContentOffset(CGPoint(x: contentOffset, y: self.contentOffset.y), animated: true)
}
}
Now you can use it wherever you want:
collectionView.scrollToNextItem()
collectionView.scrollToPreviousItem()
By far this is the best approach i have encountered, the trick is to scroll to the frame which is containing next objects. Please refer the code
/* -------------- display previous friends action ----------------*/
@IBAction func actionPreviousFriends(_ sender: Any) {
let collectionBounds = self.collectionView.bounds
let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width))
self.moveToFrame(contentOffset: contentOffset)
}
/* -------------- display next friends action ----------------*/
@IBAction func actionNextFriends(_ sender: Any) {
let collectionBounds = self.collectionView.bounds
let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width))
self.moveToFrame(contentOffset: contentOffset)
}
func moveToFrame(contentOffset : CGFloat) {
let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height)
self.collectionView.scrollRectToVisible(frame, animated: true)
}
There are two methods that could help you achieve this:
func scrollToItemAtIndexPath(indexPath: NSIndexPath,
atScrollPosition scrollPosition: UICollectionViewScrollPosition,
animated animated: Bool)
or
func setContentOffset(contentOffset: CGPoint,
animated animated: Bool)
Both are on UICollectionView
so you can use whatever seems more convenient. To create a custom animation for this however is more difficult. A quick solution depending on what you need could be this answer.
Swift 4, iOS 11:
// UICollectionView method
func scrollToItem(at indexPath: IndexPath,
at scrollPosition: UICollectionViewScrollPosition,
animated: Bool)
// UIScrollView method
func setContentOffset(_ contentOffset: CGPoint, animated: Bool)