Skip to content

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

License

Notifications You must be signed in to change notification settings

hugehoge/Snappable

Repository files navigation

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 depends on Introspect for SwiftUI due to detect the behavior of scrolling from UIScrollView, so this is fragile on iOS or SwiftUI upates.

Installation

Swift Package Manager

.package(
  url: "https://github.com/hugehoge/Snappable.git",
  .upToNextMinor(from: "0.1.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))

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published