UIScrollView.swift 974 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // UIScrollView.swift
  3. // Mastodon
  4. //
  5. // Created by sxiaojian on 2021/3/15.
  6. //
  7. import UIKit
  8. extension UIScrollView {
  9. public enum ScrollDirection {
  10. case top
  11. case bottom
  12. case left
  13. case right
  14. }
  15. public func scroll(to direction: ScrollDirection, animated: Bool) {
  16. let offset: CGPoint
  17. switch direction {
  18. case .top:
  19. offset = CGPoint(x: contentOffset.x, y: -adjustedContentInset.top)
  20. case .bottom:
  21. offset = CGPoint(x: contentOffset.x, y: max(-adjustedContentInset.top, contentSize.height - frame.height + adjustedContentInset.bottom))
  22. case .left:
  23. offset = CGPoint(x: -adjustedContentInset.left, y: contentOffset.y)
  24. case .right:
  25. offset = CGPoint(x: max(-adjustedContentInset.left, contentSize.width - frame.width + adjustedContentInset.right), y: contentOffset.y)
  26. }
  27. setContentOffset(offset, animated: animated)
  28. }
  29. }