Giter Club home page Giter Club logo

swiftui-refresher's Introduction

Refresher

A customizable, native Swift UI refresh control for iOS 14+

Why?

  • the native SwiftUI refresh control only works on iOS 15+
  • the native UIKit refresh control works with ugly wrappers, but has buggy behavior with navigation views
  • I needed a refresh control that could accomodate an overlay (such as appearing on top of a static image)
  • This one is very customizable

See it in action

If you want to see it in a real app, check out dateit

Usage

First add the package to your project.

import Refresher 

struct DetailsView: View {
    @State var refreshed = 0

    var body: some View {
        ScrollView {
            Text("Details!")
            Text("Refreshed: \(refreshed)")
        }
        .refresher { // Called when pulled to refresh
            await Task.sleep(seconds: 2)
            refreshed += 1
        }
    }
}

Features

  • async/await compatible - even on iOS 14
  • completion callback also supported for DispatchQueue operations
  • .default and .system styles (see below for details)
  • customizable refresh spinner (see below for example)

Examples and usage

See: Examples for a full sample project with multiple implementations

Navigation view

Navigation

Refresher plays nice with both Navigation views and navigation subviews.

Subview

Detail view with overlay

Refresher supports an overlay mode to show a refresh indicator over fixed position content

.refresher(overlay: true)

Overlay

System style

Refresher's default animation is designed to be more flexible that the system animation style. If you want Refresher to behave more like they system refresh control, you can change the style:

.refresher(style: .system) { done in

System

Customization

Refresher can take a custom spinner view. Your custom view will get a binding instances of the refresher state that contains useful properties for managing animations and translations. Here is a custom spinner that shows an emoji:

public struct EmojiRefreshView: View {
    @Binding var state: RefresherState
    @State private var angle: Double = 0.0
    @State private var isAnimating = false
    
    var foreverAnimation: Animation {
        Animation.linear(duration: 1.0)
            .repeatForever(autoreverses: false)
    }
    
    public var body: some View {
        VStack {
            switch state.mode {
            case .notRefreshing:
                Text("๐Ÿคช")
                    .onAppear {
                        isAnimating = false
                    }
            case .pulling:
                Text("๐Ÿ˜ฏ")
                    .rotationEffect(.degrees(360 * state.dragPosition))
            case .refreshing:
                Text("๐Ÿ˜‚")
                    .rotationEffect(.degrees(self.isAnimating ? 360.0 : 0.0))
                        .onAppear {
                            withAnimation(foreverAnimation) {
                                isAnimating = true
                            }
                    }
            }
        }
        .scaleEffect(2)
    }
}

Add the custom refresherView:

.refresher(refreshView: EmojiRefreshView.init ) { done in

Custom

Completion handler

If you prefer to call a completion to stop the refresher:

.refresher(style: .system) { done in
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
        refreshed += 1
        done() // Call done to stop the refresher
    }
}

swiftui-refresher's People

Contributors

gh123man avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.