iosswiftuitableviewuikitcontentoffset

How to get the scroll position of a UITableView in real time


I need to get the scroll position of a table view in real time. What I am currently doing is:

func animationOffset() {
    let offset = tableView.contentOffset.y
}

This function is connected to a timer

timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: #selector(TimelineViewController.animationOffset), userInfo: nil, repeats: true)

This code works fine if the user lifts their finger off the screen, but if they don't and keep scrolling, the offset will never be updated and my app will not run properly.


Solution

  • Small gotcha - in Swift 3 it's

    func scrollViewDidScroll(_ scrollView: UIScrollView)
    

    and the below is not going to be triggered :

    func scrollViewDidScroll(scrollView: UIScrollView)