Git Product home page Git Product logo

swiftuisnappingscrollview's Introduction

SwiftUI Snapping ScrollView

Add snapping behaviour to a SwiftUI ScrollView.

Demo

SnappingScrollView

A scrollable view that supports snapping.

Usage

SnappingScrollView(.vertical) {
    //Header view
        .snappingScrollAnchor(.bounds)
            
    //Content views
}

Parameters

  • axis: The scroll view's scrollable axis. The default axis is the vertical axis.
  • decelerationRate: A floating-point value that determines the rate of deceleration after the user ends dragging. The default value for this parameter is .normal.
  • showsIndicators: A Boolean value that indicates whether the scroll view displays the scrollable component of the content offset, in a way suitable for the platform. The default value for this parameter is true.
  • content: The view builder that creates the scrollable view.

snappingScrollAnchor

A function that sets the scroll snapping anchor rect for a view.

Avoid setting the scroll snapping anchor rect on a child of a lazy view, such as a LazyHGrid, LazyVGrid, LazyHStack or LazyVStack.

Parameters

  • source: The source of the anchor rect.

Advanced Usage

SnappingScrollView can provide paging behaviour when initialised with a decelerationRate of .fast. Add the pages to a stack view with spacing set to 0. Any desired spacing should be added as padding to the edge of each page view:

SnappingScrollView(.horizontal, decelerationRate: .fast) {
    HStack(spacing: 0) {
        ForEach(...) {
            //Page view
                .padding(.trailing)
                .snappingScrollAnchor(.bounds)
        }
    }
}

snappingScrollAnchor should not be used on a child of a lazy view. Instead, use non-lazy parent views, and use onVisible to load data or update subviews when the view's bounds move into the visible frame. Provide padding to increase the distance from the visible frame that the action is called.

Requirements

  • iOS 13.0+
  • Xcode 12.0+

Installation

Contact

@ciaranrobrien on Twitter.

swiftuisnappingscrollview's People

Contributors

ciaranrobrien avatar harshvishu avatar

Watchers

James Cloos 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.