Giter Club home page Giter Club logo

snappable's Introduction

Snappable

A set of SwiftUI custom modifiers to make the ScrollView snappable.

The goal of this library is to provide an easy way to implement Views such as carousels and slideshows.

Demo

Carousel demo Vertical demo

Requirements

  • iOS 14.0+
  • Swift 5.3+

Note

Snappable includes the copied code from Introspect for SwiftUI (#17) due to detect the behavior of scrolling from UIScrollView. So this library would be fragile on iOS or SwiftUI updates.

Installation

Swift Package Manager

.package(
  url: "https://github.com/hugehoge/Snappable.git",
  .upToNextMinor(from: "0.3.0")
)

CocoaPods

pod 'Snappable', '~> 0.3.0'

Usage

Basic

struct ContentView: View {
  @State private var items: [Item]

  var body: some View {
    ScrollView(.horiaontal) {
      LazyHStack {
        ForEach(items, id: \.self) { item in
          ItemView(item)
            .snapID(item)  // Step 1
        }
      }
    }
    .snappable()  // Step 2
  }
}
  1. Added .snapID(_:) modifier to items in ScrollView
    • snapID applies .id(_:) modifier internally
  2. Added .snappable(_:mode:) modifier to ScrollView

Options

Alignment

The snap anchor point can be set as an option.

.snappable(alignment: .leading)

Available alignment parameters are below:

  • .top
  • .leading
  • .center
  • .trailing
  • .bottom

SnapMode

You can determine the snap timing after the end of the drag with following parameters.

  • .afterScrolling
  • .immediately

Both parameters are set together with scrolling deceleration rate.

.snappable(alignment: .center, mode: .afterScolling(decelerationRate: .fast))
.snappable(alignment: .center, mode: .immediately(decelerationRate: .normal, withFlick: false))

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.