RefreshControlKit 0.0.3

RefreshControlKit 0.0.3

Maintained by hirotakan.



  • By
  • hirotakan

RefreshControlKit

Swift 5 License Carthage compatible Cocoapods SwiftPM Platforms

RefreshControlKit is a library for custom RefreshControl that can be used like UIRefreshControl.

Requirements

  • Swift 5 or later
  • iOS 12 or later

Usage

RefreshControl can be used by using @RefreshControlling of the Property Wrappers.

import UIKit
import RefreshControlKit

class ViewController: UIViewController {

    @RefreshControlling(wrappedValue: nil, view: CustomView())
    @IBOutlet private var myScrollingView: UIScrollView!

    // or

    @RefreshControlling(view: CustomView())
    private var myScrollingView = UIScrollView()

Since the projected value is RefreshControl, you can use it like UIRefreshControl by doing the following.

func configureRefreshControl () {

    $myScrollingView.addTarget(self, action:
                        #selector(handleRefreshControl),
                        for: .valueChanged)
}
    
@objc func handleRefreshControl() {
    // Update your content…

    // Dismiss the refresh control.
    DispatchQueue.main.async {
        self.$myScrollingView.endRefreshing()
    }
}

You can also use RefreshControl directly.

let refreshControl = RefreshControl(view: CustomView())
func configureRefreshControl () {

    myScrollingView.addRefreshControl(refreshControl)

    refreshControl.addTarget(self, action:
                      #selector(handleRefreshControl),
                      for: .valueChanged))
}

You can also start refreshing programmatically:

refreshControl.beginRefreshing()

Creating custom RefreshControlView

RefreshControl height depends on the height of the custom view.

To create a custom view, the following conditions must be met.

class CustomView: UIView, RefreshControlView {

    func willRefresh() {
        // Something to do before refreshing.
    }

    func didRefresh() {
        // Something to do after refreshing.
    }

    func didScroll(_ progress: RefreshControl.Progress) {
        // Something to do while scrolling.
        // `Progress` expresses the progress to the height of the trigger as 0.0 to 1.0.
    }
}

See the sample code for details.

Configuration

public struct Configuration {
    public var layout: Layout
    public var trigger: Trigger
}

Layout and Trigger can be customized.

Layout

Layout can be selected as top or bottom.

public enum Layout {
    /// The top of the `RefreshControl` is anchored at the top of the `ScrollView` frame.
    case top
    /// The bottom of the `RefreshControl` is anchored above the content of the `ScrollView`.
    case bottom
}
@RefreshControlling(view: CustomView(), configuration: .init(layout: .bottom))
private var myScrollingView = UIScrollView()
top bottom

Trigger

Height

@RefreshControlling(view: CustomView(), configuration: .init(trigger: .init(height: 50)))
private var myScrollingView = UIScrollView()

You can specify the height at which the refreshing starts. The default is the height of the custom view.

Event

Event can be selected as dragging or released.

public enum Event {
    /// When it is pulled to the trigger height, `beginRefreshing` is called.
    case dragging
    /// If the height of the trigger is exceeded at the time of release, `beginRefreshing` is called.
    case released
}
@RefreshControlling(view: CustomView(), configuration: .init(trigger: .init(event: .released)))
private var myScrollingView = UIScrollView()
dragging released

Installation

SwiftPM

Add RefreshControlKit as a dependency:

import PackageDescription

let package = Package(
    name: "YourApp",
    dependencies: [
        .Package(url: "https://github.com/hirotakan/RefreshControlKit.git", majorVersion: 0),
    ]
)

CocoaPods

Add the following to your Podfile:

pod 'RefreshControlKit'

Carthage

Add the following to your Cartfile:

github "hirotakan/RefreshControlKit"

License

RefreshControlKit is released under the MIT license. See LICENSE for details.