Search code examples
iosswiftuirefreshcontrol

addTarget to UIRefreshControl()


I have made a UIRefreshControl:

refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "refresh")
refresher.addTarget(self, action: "refreshed", forControlEvents: UIControlEvents.ValueChanged)
refresher.addTarget(self, action: "refreshed", forControlEvents: UIControlEvents.ApplicationReserved)
refresher.addTarget(self, action: "refreshed", forControlEvents: UIControlEvents."scrolled down to bottom")  

It is a refresher and works fine, but I want to add an action which will make it refresh when I scroll down to the bottom of my tableView, something like I have done with ValueChanged, but for the bottom


Solution

  • You can use the built in scrollViewDidEndDecelerating

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height
        if bottomEdge >= scrollView.contentSize.height {
            // Reached bottom, do your refresh
            print("Bottom")
        }
    }
    

    When the scrollView has ended it´s decelerating and you´re at the bottom, then you can call your refresher.