UIRefreshcontrol jitters when pulled down and held

后端 未结 7 601
野趣味
野趣味 2021-01-01 23:21

I have created a UIRefreshcontrol in my tableviewcontroller as follows in the viewdidload method :

    refresh = [UIRefreshControl.alloc init];
    refresh.a         


        
7条回答
  •  無奈伤痛
    2021-01-01 23:58

    So I just came across this same problem, and I came up with a solution. Basically, if you pull your tableView down far enough and hold it, the ValueChanged control event will fire before the user releases the touch. If you refresh your data and reload the tableView at this point, the table will jump up.

    To combat this, I ended up refreshing the table independently of the ValueChanged event. Instead, I used that event, along with the scrollView delegate methods, to only refresh the table after the user had let go.

    So first, set up your refreshControl in viewDidLoad.

    override func viewDidLoad()
    {
        super.viewDidLoad()
    
        refreshControl = UIRefreshControl()
        refreshControl?.addTarget(self, action: "refreshControlChanged", forControlEvents: UIControlEvents.ValueChanged)
    }
    

    The ValueChanged method is simple. If we've already stopped dragging, which happens after a quick swipe down, just refresh the table using your own custom method.

    internal func refreshControlChanged()
    {
        if !tableView.dragging
        {
            refresh()
        }
    }
    

    Then, you need to implement didEndDragging. If you've pulled down far enough, the refreshControl will be refreshing, so call refresh. Otherwise, you either haven't pulled down far enough, you were dragging on a different part of the table, or you dragged so quickly that the refreshControl ValueChanged event has yet to fire, and it will refresh the table there.

    override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
    {
        if refreshControl?.refreshing == true
        {
            refresh()
        }
    }
    

    And that's it. The table won't get reloaded until after you've stopped dragging it, which avoids the jittery jump that you noticed. I know this is a year old, but I hope this helps anyone else coming across this on Google.

提交回复
热议问题