How Do I Shorten the Pull Distance on UIRefreshControl to Activate the Pull to Refresh Action?

后端 未结 5 1071
傲寒
傲寒 2021-02-07 09:27

Hey StackOverflow People,

I\'ve been trying to figure out this question for some time now but to no avail and I need some help. I have a UITableView close t

相关标签:
5条回答
  • 2021-02-07 09:42

    you can make it right with some modification

    lazy var refreshControl: UIRefreshControl = {
            let refreshControl = UIRefreshControl()
            refreshControl.tintColor = UIColor.red
            return refreshControl
        }()
    
    //refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
    

    every PullToRefresh must have couple lines of code like this, that handleRefresh function, do whatever you need to refresh the page.

    you just need to comment out addTarget line and add this function to your code ```

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if scrollView.contentOffset.y < -80 { //change 80 to whatever you want
                if  !self.refreshControl.isRefreshing {
                    handleRefresh()
                }
            }
        }
    

    I wrote this code with the help of Ashkan Ghodrat's answer

    0 讨论(0)
  • 2021-02-07 09:46

    you can still use refreshControl but with some modifications!

    add these code to your viewController:

    var canRefresh = true
    
    override func scrollViewDidScroll(scrollView: UIScrollView) {
    
        if scrollView.contentOffset.y < -100 { //change 100 to whatever you want
    
            if canRefresh && !self.refreshControl.refreshing {
    
                self.canRefresh = false
                self.refreshControl.beginRefreshing()
    
                self.refresh() // your viewController refresh function
            }
        }else if scrollView.contentOffset.y >= 0 { 
    
            self.canRefresh = true
        }
    }
    

    and as usual in the end of your refresh logic in self.refresh() function add :

       self.refreshControl.endRefreshing()
    
    0 讨论(0)
  • 2021-02-07 09:47

    As per the Apple Docs, I don't see any way to modify UIRefreshControl parameters.
    link: https://developer.apple.com/library/ios/documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

    Use a third-party component like ODRefreshControl (to customize the scroll-distance in order to activate the refresh, modify the #define kMaxDistance constant).

    or...

    Don't use the UIRefreshControl and instead implement your own logic in the -scrollViewDidScroll method like:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) {
            //refresh logic
        }
    }
    
    0 讨论(0)
  • 2021-02-07 10:01

    For Swift 3.2 and above :

    var canRefresh = true
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y < -100 {
            if canRefresh && !self.refreshControl.isRefreshing {
                self.canRefresh = false
                self.refreshControl.beginRefreshing()
                self.handleRefresh()
            }
        } else if scrollView.contentOffset.y >= 0 {
            self.canRefresh = true
        }
    }
    
    0 讨论(0)
  • 2021-02-07 10:03

    One line code

    The value for that is _snappingHeight and you can set it to what ever you need like:

    refreshControl.setValue(100, forKey: "_snappingHeight")
    

    Note that this is a private API and although it’s tested on the appstore, it may reject in the future.

    0 讨论(0)
提交回复
热议问题