BONavigationPullToRefresh 0.9

BONavigationPullToRefresh 0.9

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2016
SPMSupports SPM

Maintained by Bruno Oliveira.



BONavigationPullToRefresh

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

The Demo project requires iOS9

Requirements

Installation

BONavigationPullToRefresh is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "BONavigationPullToRefresh"

To start using simply configure the current ViewController import:

import BONavigationPullToRefresh

Simple usecase

class ExampleViewController: UIViewController, NavigationPullRefreshable {
  let fakeLoadingTime = dispatch_time(DISPATCH_TIME_NOW, Int64(4 * NSEC_PER_SEC))
  @IBOutlet weak var scrollView: UIScrollView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Maximum height for the loading view
    let maxHeight: CGFloat = self.navigationController?.navigationBar.absoluteHeight ?? 10

    // Configurations to animate the default RefreshableView
    let configurations = DefaultRefreshingViewConfigurations(maxHeight: maxHeight,
                                                             image: UIImage(named: "sample"))

    let refreshableView = DefaultRefreshingView(configurations: configurations)


    addNavigationPullToRefresh(toScrollView: self.scrollView, refreshingView: refreshableView) {
      let fakeLoadingTime = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * NSEC_PER_SEC))
      dispatch_after(fakeLoadingTime, dispatch_get_main_queue()) {
        self.endRefreshing()
      }
    }
  }
}

RefreshingView

The refreshingView is a UIView that conforms to protocol RefreshableView:

public protocol RefreshableView {
  // View will start refreshing
  func startRefreshing()

  // Loading has beed canceled due to ViewController disapear
  func cancelRefreshing()

  // Loading has finished successfully
  func endRefreshing()

  // View needs to be updated to the percentage of loading given (bettween 0 - 1)
  func updateLoadingItem(percentage: CGFloat)
}

You can use the DefaultRefreshingView that only updates its alpha as it is shown in the example.

DefaultRefreshingView

  // configurations is a DefaultRefreshingViewConfigurations
  DefaultRefreshingView(configurations: configurations)

DefaultRefreshingViewConfigurations

public struct DefaultRefreshingViewConfigurations {

// Maximum view Size
var maxHeight: CGFloat = 80

  // View inset
  var inset = CGPoint.zero

  // Image to be shown
  var image: UIImage?

  // Scaling option of the UIImageView
  var imageOptions = UIViewContentMode.ScaleToFill

  // Animation time
  var animationTime: NSTimeInterval = 1

  // Fade animation Time
  var animationFadeTime: NSTimeInterval = 0.5
}

End refreshing

self.endRefreshing()

Lifecycle

To keep the viewControllers stack lifecycle its needed to call three more methods on your ViewController:

override func viewWillAppear(animated: Bool) {
  super.viewWillAppear(animated)
  viewControllerWillShow()
}

override func viewDidAppear(animated: Bool) {
  super.viewDidAppear(animated)
  viewControllerDidShow()
}

override func viewWillDisappear(animated: Bool) {
  super.viewWillDisappear(animated)
  viewControllerWillDisappear()
}

All the supported methods in the NavigationPullRefreshable are :

public protocol NavigationPullRefreshable {
  // Configure the NavigationPullToRefresh
  func addNavigationPullToRefresh(toScrollView scrollView: UIScrollView,
                                               refreshingView: RefreshableView,
                                               startLoading: () -> Void)

  // You must call when the ViewController appears to pause loader when viewController is pushed
  func viewControllerWillShow()

  // You must call when the ViewController didAppear to set the refreshable view below the title
  func viewControllerDidShow()

  // You must call when the ViewController will disappear to pause loader when viewController is popped
  // or pushed
  func viewControllerWillDisappear()

  func endRefreshing()
}

Configurations

  // Distance needed to trigger refresh
  public var triggerDistance: CGFloat = 80

Author

Bruno Oliveira, [email protected]

License

BONavigationPullToRefresh is available under the MIT license. See the LICENSE file for more info.