Skip to content

Instantly share code, notes, and snippets.

@hsleedevelop
Last active December 4, 2018 01:50
Show Gist options
  • Save hsleedevelop/a32ab9d19cbbcefd68c9e2827e910ba2 to your computer and use it in GitHub Desktop.
Save hsleedevelop/a32ab9d19cbbcefd68c9e2827e910ba2 to your computer and use it in GitHub Desktop.
infinite scrolling of collectionView
func infinateLoop(scrollView: UIScrollView) {
var index = Int((scrollView.contentOffset.x)/(scrollView.frame.width))
guard currentIndex != index, let banners = banners else {
//currentIndex = index
return
}
currentIndex = index
if index <= 0 {
index = banners.count - 1
let adjust = isDragging ? 1 : 0
scrollView.setContentOffset(CGPoint(x: (scrollView.frame.width + (scrollView.frame.width / 2 - adjust)) * CGFloat(banners.count), y: 0), animated: false)
} else if index >= banners.count + 1 {
index = 0
scrollView.setContentOffset(CGPoint(x: (scrollView.frame.width), y: 0), animated: false)
} else {
index -= 1
}
Log.d(">>>>>>>>>> index = \(index), currentIndex = \(currentIndex), offset=\(scrollView.contentOffset.x)")
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
infinateLoop(scrollView: scrollView)
isDragging = false
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
infinateLoop(scrollView: scrollView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment