Swiftpack.co - zyvv/Refresher as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by zyvv.
zyvv/Refresher 1.1
A pull-to-refresh component that can customize animation according to the pull-down state.
⭐️ 1
🕓 2 years ago
iOS
.package(url: "https://github.com/zyvv/Refresher.git", from: "1.1")

Refresher

Refresher is a pull-to-refresh component that can customize animation according to the pull-down state. The core code based on MJRefresh and PullToRefresh.

Refresher

Requirements

  • iOS 11.0+
  • Swift 5.0+

Installation

CocoaPods

use_frameworks!
pod 'Refresher_Swift'

Carthage

github "zyvv/Refresher"

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/zyvv/Refresher", .upToNextMajor(from: "1.0.0"))
]

Usage

Quick Start

If you installed via CocoaPods:

import Refresher_Swift

Otherwise:

import Refresher

Then:

class MyViewController: UICollectionViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // pull-down refresh
        collectionView.topRefresher = Refresher {
            // fetchLatestData()
        }
        
        // pull-up refresh
        collectionView.bottomRefresher = Refresher {
            // fetchData(page: Int)
        }
    }
}

Trigger Refresher to refreshing with code:

collectionView.topRefresher?.beginRefreshing()

End Refresher refreshing:

collectionView.topRefresher?.endRefreshing()

Disable Refresher:

collectionView.topRefresher?.isEnable = false

Customize

At first, Create a custom view that conforms Refreshable protocol:

class CustomAnimateView: UIView, Refreshable {

    func animate(_ state: Refresher.State) {
        switch state {

        // Idle state. Back to the initial state.
        case .idle: idleState()

        // Animation being pulled down.
        // progress: 0 -> 1
        case .pulling(let progress): pullingAnimation(progress)

        // Animation to be refreshed.
        // overOffset: The pull-down offset of the scrollview in this state.
        case .willRefresh(let overOffset): willRefreshAnimation(overOffset)
        
        // Animation being refreshed.
        case .refreshing: refreshingAnimation()
            
        // Rebound animation after refresh.
        // progress: 0 -> 1
        case .rebounding(let progress): pullingAnimation(1-progress)
        }
    }
}

Then, initialize Refresher:

collectionView.topRefresher = Refresher(CustomAnimateView()) {
    // fetchLatestData()
}

Pull-up to Load More

The difference with pull-down refresh is that the state of pull-up refresh is directly from .idle to .refreshing after bottomRefresher appears.

Pull-up refresh can be ended like pull-down refresh, or a no-more-data view can be added after the end. After the view is added, bottomRefresher will be disabled.

func noMoreLabel() -> UILabel {
    let label = UILabel()
    label.text = "No More Data."
    label.textColor = .placeholderText
    label.textAlignment = .center
    label.font = UIFont.systemFont(ofSize: 12)
    label.frame.size.height = 30
    return label
}

func endRefreshing(count: Int) {
    collectionView.bottomRefresher?.endRefreshing(count == 0 ? noMoreLabel() : nil)
}

License

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

GitHub

link
Stars: 1
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics