You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.1 KiB

5 years ago
  1. //
  2. // UIScrollView+reachedBottom.swift
  3. // RxSwiftExt
  4. //
  5. // Created by Anton Nazarov on 09/05/2019.
  6. // Copyright © 2019 RxSwift Community. All rights reserved.
  7. //
  8. #if os(iOS)
  9. import UIKit
  10. import RxSwift
  11. import RxCocoa
  12. public extension Reactive where Base: UIScrollView {
  13. /**
  14. Shows if the bottom of the UIScrollView is reached.
  15. - parameter offset: A threshhold indicating the bottom of the UIScrollView.
  16. - returns: ControlEvent that emits when the bottom of the base UIScrollView is reached.
  17. */
  18. func reachedBottom(offset: CGFloat = 0.0) -> ControlEvent<Void> {
  19. let source = contentOffset.map { contentOffset in
  20. let visibleHeight = self.base.frame.height - self.base.contentInset.top - self.base.contentInset.bottom
  21. let y = contentOffset.y + self.base.contentInset.top
  22. let threshold = max(offset, self.base.contentSize.height - visibleHeight)
  23. return y >= threshold
  24. }
  25. .distinctUntilChanged()
  26. .filter { $0 }
  27. .map { _ in () }
  28. return ControlEvent(events: source)
  29. }
  30. }
  31. #endif