How do I programmatically slide the UITableView down to reveal the underlying UIRefreshControl

后端 未结 4 1857
天涯浪人
天涯浪人 2021-02-04 01:06

How can I reveal the UIRefreshControl when I update the table programmatically? Using [self.refreshControl beginRefreshing] make the spinner animate but does not reveal it.

4条回答
  •  遥遥无期
    2021-02-04 01:38

    For Swift 3, this is what I have based on Peter Lapisu's answer:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.refreshControl?.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged)
        // ...
    }
    
    func refresh(sender:AnyObject) {
        self.refreshControl?.beginRefreshing()
    
        if let yOffsetTable = self.tableView?.contentOffset.y {
            if yOffsetTable < CGFloat(Float.ulpOfOne) {
                UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: {
                    if let refreshControlHeight = self.refreshControl?.frame.height {
                        self.tableView?.contentOffset = CGPoint(x: 0, y: -refreshControlHeight)
                    }
                }, completion: nil)
            }
        }
    }
    

提交回复
热议问题