Last active
April 17, 2018 15:19
-
-
Save Pegolon/298203f8508b0f3a50a35f5caa595280 to your computer and use it in GitHub Desktop.
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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scrolls all the way down and up again and then stops the timer.