Last active
April 24, 2024 06:03
-
-
Save benigumocom/29ca624837d5fdf0f025c6b9df0b41fa to your computer and use it in GitHub Desktop.
【SwiftUI】ScrollView や List を snap する 👉 https://android.benigumo.com/20240424/snap-list/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct SnapList: View { | |
private let colors: [Color] = [ | |
.white, .black, .red, .blue, .yellow, .green | |
] | |
var body: some View { | |
ScrollView { | |
LazyVStack(spacing: 0) { | |
ForEach(0..<30, id: \.self) { i in | |
colors[i % 6] | |
.frame(height: 100) | |
} | |
} | |
.scrollTargetLayout() // * | |
} | |
.scrollTargetBehavior(.viewAligned) // * | |
.frame(width: 300, height: 300) | |
.border(.gray) | |
} | |
} | |
#Preview { | |
SnapList() | |
} |
Author
benigumocom
commented
Apr 24, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment