Created
December 6, 2017 17:40
-
-
Save srstanic/ecaf91f6947ff0426c2bd736a6ab35e1 to your computer and use it in GitHub Desktop.
Animated scroll to left or right for distance
This file contains hidden or 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
| 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