Last active
May 8, 2023 03:51
-
-
Save sgr-ksmt/3331bf38dc821eecdc695465347ddee3 to your computer and use it in GitHub Desktop.
UIScrollView: scroll top/bottom/left/right perfectly! http://blog.sgr-ksmt.org/2016/11/11/uiscrollview_scroll_extension/
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
extension UIScrollView { | |
public enum ScrollDirection { | |
case top | |
case bottom | |
case left | |
case right | |
} | |
public func scroll(to direction: ScrollDirection, animated: Bool) { | |
let offset: CGPoint | |
switch direction { | |
case .top: | |
offset = CGPoint(x: contentOffset.x, y: -contentInset.top) | |
case .bottom: | |
offset = CGPoint(x: contentOffset.x, y: max(-contentInset.top, contentSize.height - frame.height + contentInset.bottom)) | |
case .left: | |
offset = CGPoint(x: -contentInset.left, y: contentOffset.y) | |
case .right: | |
offset = CGPoint(x: max(-contentInset.left, contentSize.width - frame.width + contentInset.right), y: contentOffset.y) | |
} | |
setContentOffset(offset, animated: animated) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage