Skip to content

Poppin-Technologies/magnetic-scroll

Repository files navigation

thmb-modified

MagneticScroll

A Library that adds a sticky behavior to the SwiftUI's ScrollView, while triggering a smooth haptic feedback as you scroll through the views.

Installation

Requires iOS 14.0+

MagneticScroll currently can only be installed through the Swift Package Manager.

Swift Package Manager
Add the Package URL:

https://github.com/Poppin-Technologies/magnetic-scroll.git

Showcase

⚛️ Regular Magnetic Scroll

As you scroll, when the ScrollView's velocity is lesser than MagneticScrollView's velocity, magnetic scroll automatically sticks to the predicted end location.

regular

✨ Manually changing the blocks

manual

🙌 Magnetic Scroll with .matchedGeometryEffect modifier

mached

🔥 MagneticCarousel

If you set the velocityThreshold to .infinity, MagneticScroll becomes a carousel.

carousel

Usage

MagneticScroll is designed to operate with a view called Block. For MagneticScroll to detect scroll changes, it requires your content to be wrapped within Block elements.

import SwiftUI
import MagneticScroll

struct ContentView: View {
  // If you were to set activeBlock to "second" or "first" manually, MagneticScroll would automatically scroll to the block with that id.
  @State private var activeBlock = "first"
  var body: some View {
    MagneticScrollView(activeBlock: $activeBlock) { organizer in
      Block(id: "first", height: 400, inActiveHeight: 300) { // All of these fields are optional, except the ID, but magnetic scroll works x5 better with constant heights.
        Text("Hello World")
      }
      Block(id: "second", height: 400, inActiveHeight: 300) {
        Text("Hello World")
      }
    }
  }
}

Methods

Here are the methods available for configuring the behavior of MagneticScrollView:

🖱️ changesActiveBlockOnTapGesture(_ value: Bool)

Sets whether the active block should be changed on a tap gesture.

🏁 velocityThreshold(_ threshold: Double)

Sets the velocity threshold for MagneticScrollView to react to scroll view velocity.

📳 triggersHapticFeedbackOnBlockChange(_ bool: Bool)

Sets whether haptic feedback should be triggered when the active block changes.

📳 triggersHapticFeedbackOnActiveBlockChange(_ bool: Bool)

Sets whether haptic feedback should be triggered when the active block changes.

📋 formStyle(_ bool:)

Sets whether the form style should be enabled or not.

⏳ scrollAnimationDuration(_ duration: Double)

Sets the scroll animation duration when changing the active block.

⌛ setTimeout(_ duration: Double)

Sets the timeout duration needed to change a block to another.

Organizer

MagneticScrollView gives an organizer to control the behavior of itself. Organizer contains a ScrollViewProxy so if you want to control the ScrollView itself, you can use that.

🪐 activate(with: Block.ID)

Activates a block with given ID. But doesn't scroll to it

👇🏻 scrollTo(id: Block.ID, anchor: UnitPoint)

Scrolls and activates a block with given id and anchor

👆🏻 scrollToCurrentOffset()

Scrolls to the nearest block with the current offset of the MagneticScrollView.

Apps Using MagneticScroll

Poppin - The Party Platform

logoppin

poppin

Releases

No releases published

Packages

No packages published

Languages