Code Monkey home page Code Monkey logo

pulltorefresh's Introduction

PullToRefresh

This package adds far more flexible pull-to-refresh functionality to SwiftUI, for iOS 14 and 15.

RefreshableScrollView

  • Usable in any view through a pair of view modifiers
  • Convenient RefreshableScrollView container
  • Allows full customisation of the refreshing view

Using RefreshableScrollView

Replace your ScrollView with RefreshableScrollView and add the refreshCallback: parameter.

class ViewModel {
    func startRefresh(completion: @escaping () -> Void) {
        // Do something...
        
        completion()
    }
}
RefreshableScrollView(refreshCallback: viewModel.startRefresh(completion:)) {
    YourScrollContentView()
}

Customising the refresh view

You can use any view for the refresh view; just throw in an environment variable to watch the pull to refresh state and you're done.

struct MyCustomRefreshView: View {
    @Environment(\.pullToRefreshState) var refreshState
    
    // Your custom view body goes here!
}

Then, pass it in as a parameter to RefreshableScrollView or pullToRefreshContainer().

RefreshableScrollView(refreshCallback: viewModel.startRefresh(completion:), refreshView: MyCustomRefreshView()) {
    YourScrollContentView()
}

Customising PullToRefreshView

You can easily change the indicator views for the default PullToRefreshView, too.

let refreshView = PullToRefreshView(indicatorView: Image(systemName: "arrow.clockwise.circle.fill"))

Using the modifiers

If you want to apply pull-to-refresh to a different type of view, you can use the view modifiers pullToRefreshContent() and pullToRefreshContainer. The former goes on the content (as in the thing that the user drags with their finger) and the latter goes on the container (as in the thing that stays still when the user drags the content). If your container takes multiple views, apply the content modifier to the one at the top.

MyCustomScrollView {
    Color.clear.pullToRefreshContent()
    
    ForEach(items) { item in
        YourListItemView(item)
    }
}
.pullToRefreshContainer { viewModel.startRefresh(completion: $0) }
.clipped()

pulltorefresh's People

Contributors

gormster avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

pulltorefresh's Issues

Doesn't work with List

Yes, List has native support for refreshable(_:) in iOS 15, but there's nothing for iOS 14.

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.