Skip to content

Instantly share code, notes, and snippets.

@Pegolon
Last active April 17, 2018 15:19
Show Gist options
  • Save Pegolon/298203f8508b0f3a50a35f5caa595280 to your computer and use it in GitHub Desktop.
Save Pegolon/298203f8508b0f3a50a35f5caa595280 to your computer and use it in GitHub Desktop.
var simulateScrollingTimer: Timer?
var scrollUp = false
private func simulateScrolling() {
collectionView.contentOffset = CGPoint.zero
scrollUp = false
simulateScrollingTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(simulateSwipe), userInfo: nil, repeats: true)
}
private func simulateSwipe() {
guard collectionView.contentSize.height > 0
else { return }
var currentPosition = collectionView.contentOffset
currentPosition.y += collectionView.bounds.height * (scrollUp ? -1.5 : 1.5)
if currentPosition.y > collectionView.contentSize.height {
scrollUp = true
currentPosition.y = collectionView.contentSize.height - collectionView.bounds.height
} else if currentPosition.y < 0 {
currentPosition.y = 0
if scrollUp {
simulateScrollingTimer?.invalidate()
}
}
collectionView.setContentOffset(currentPosition, animated: true)
}
@Pegolon
Copy link
Author

Pegolon commented Apr 17, 2018

Scrolls all the way down and up again and then stops the timer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment