You can achieve both goals using Key-Value Observing (KVO) to track contentOffset and contentSize view scrolling regardless of the table view delegate.
contentOffset is the scrollView's scroll count. The y value is the amount scrollable in the vertical direction.
contentSize is the total height of all rows in the table.
KVO allows you to write code that is called when a property changes on another object. You can use KVO to track changes in contentSize and contentOffset and update the user view when these values ββchange.
Here's how you can implement it in your CocoaPod:
private var ContentOffsetKVO = 0 private var ContentSizeKVO = 0 public class ScrollController: NSObject { public var customView: UIView? { didSet { updateScrollPosition() } } public var scrollView: UIScrollView? { didSet { if let view = oldValue { removeKVO(view) } if let view = scrollView { addKVO(view) updateScrollPosition() } } } deinit { if let scrollView = scrollView { removeKVO(scrollView) } } private func removeKVO(scrollView: UIScrollView) { scrollView.removeObserver( self, forKeyPath: "contentSize", context: &ContentSizeKVO ) scrollView.removeObserver( self, forKeyPath: "contentOffset", context: &ContentOffsetKVO ) } private func addKVO(scrollView: UIScrollView) { scrollView.addObserver( self, forKeyPath: "contentSize", options: [.Initial, .New], context: &ContentSizeKVO ) scrollView.addObserver( self, forKeyPath: "contentOffset", options: [.Initial, .New], context: &ContentOffsetKVO ) } public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { switch keyPath { case .Some("contentSize"), .Some("contentOffset"): self.updateScrollPosition() default: super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) } } private func updateScrollPosition() { guard let scrollView = scrollView, let customView = customView else { return }
To use this:
let scrollController = ScrollController() override func viewDidLoad() { super.viewDidLoad()
source share