A Library that adds a sticky behavior to the SwiftUI
's ScrollView
, while triggering a smooth haptic feedback as you scroll through the views.
Requires iOS 14.0+
MagneticScroll currently can only be installed through the Swift Package Manager.
Swift Package Manager
Add the Package URL: |
|
As you scroll, when the ScrollView
's velocity is lesser than MagneticScrollView
's velocity, magnetic scroll automatically sticks to the predicted end location.
If you set the velocityThreshold
to .infinity
, MagneticScroll becomes a carousel.
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")
}
}
}
}
Here are the methods available for configuring the behavior of MagneticScrollView
:
Sets whether the active block should be changed on a tap gesture.
Sets the velocity threshold for MagneticScrollView
to react to scroll view velocity.
Sets whether haptic feedback should be triggered when the active block changes.
Sets whether haptic feedback should be triggered when the active block changes.
Sets whether the form style should be enabled or not.
Sets the scroll animation duration when changing the active block.
Sets the timeout duration needed to change a block to another.
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.
Activates a block with given ID. But doesn't scroll to it
Scrolls and activates a block with given id and anchor
Scrolls to the nearest block with the current offset of the MagneticScrollView
.