Skip to content

Instantly share code, notes, and snippets.

@srstanic
Created December 6, 2017 17:40
Show Gist options
  • Select an option

  • Save srstanic/ecaf91f6947ff0426c2bd736a6ab35e1 to your computer and use it in GitHub Desktop.

Select an option

Save srstanic/ecaf91f6947ff0426c2bd736a6ab35e1 to your computer and use it in GitHub Desktop.
Animated scroll to left or right for distance
extension UIScrollView {
func scrollToLeft(for distance: CGFloat, withAnimationDuration animationDuration: TimeInterval = 0.3) {
var newOffset = self.contentOffset.x - distance
if newOffset < 0 {
newOffset = 0
}
UIView.animate(withDuration: animationDuration) {
self.contentOffset.x = newOffset
}
}
func scrollToRight(for distance: CGFloat, withAnimationDuration animationDuration: TimeInterval = 0.3) {
var newOffset = self.contentOffset.x + distance
let maxOffset = self.contentSize.width - self.bounds.width
if newOffset > maxOffset {
newOffset = maxOffset
}
UIView.animate(withDuration: 0.3) {
self.contentOffset.x = newOffset
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment