swift pull to refresh

ⅰ亾dé卋堺 提交于 2019-11-29 14:38:53
self.scrollView.scrollEnabled = true
self.scrollView.alwaysBounceVertical = true

var alwaysBounceVertical: Bool // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically

iOS 10 Update

alwaysBounceVertical is not entirely true, and refreshControl has been introduced:

  1. All iOS versions: bounces is always required if contentSize is smaller than frame
  2. Pre iOS 10 versions: alwaysBounceVertical is also required for small content
  3. iOS 10+: notify the UIScrollView of the existence of a UIRefreshControl using refreshControl

- iOS 10

UIRefreshControl is now supported by UIScrollView, using refreshControl just like UITableView in previous OS.
This means that the drag & pull down experience, is flawless, without drift.
Follow the tap + drag on the white arrow in the animation below: they stay in sync

↻ replay animation

- iOS 9 and earlier

You can add a UIRefreshControl manually to a UIScrollView, but that view has no awareness of such an element, and the pull to refresh tends to drift.
Notice how much harder it is to pull to refresh on the animation below: scrolling of the white arrow drifts, and requires a much greater distance to trigger the control

↻ replay animation


Bypass refreshControl OS discrepancies with this scroll view extension:

var _refreshControl : UIRefreshControl? {
    get {
        if #available(iOS 10.0, *) {
            return refreshControl
        } else {
            return subviews.first(where: { (view: UIView) -> Bool in
                view is UIRefreshControl
            }) as? UIRefreshControl
        }
    }

    set {
        if #available(iOS 10.0, *) {
            refreshControl = newValue
        } else {
            // Unique instance of UIRefreshControl added to subviews
            if let oldValue = _refreshControl {
                oldValue.removeFromSuperview()
            }
            if let newValue = newValue {
                insertSubview(newValue, at: 0)
            }
        }
    }
}

► Find this solution on GitHub.

Ketan

Yes We can use refresh control to UIScrollView, UITableview, UicollectionVIew..

Here is the code for PULL TO REFRESH,

var refreshControl: UIRefreshControl!
override func viewDidLoad()  {
    super.viewDidLoad()

    scrollView.alwaysBounceVertical = true
    scrollView.bounces  = true
    refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(didPullToRefresh), for: .valueChanged)
    self.scrollView.addSubview(refreshControl)
 }

@objc func didPullToRefresh() {

   print("Refersh")

  // For End refrshing
  refreshControl?.endRefreshing()  


 }

This UIRefreshControl is not supposed to work for UIScrollView. It is supposed to be linked with a table through an associated table view controller object.

Note: Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in a different context can result in undefined behavior.

UIRefreshControl Class Reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!