Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Created May 10, 2024 21:44
Show Gist options
  • Save Koshimizu-Takehito/98abbbc3da709de9f23104abf0a1676b to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/98abbbc3da709de9f23104abf0a1676b to your computer and use it in GitHub Desktop.
EditMode Sample
import SwiftUI
struct ContentView: View {
@State var mode = EditMode.inactive
var body: some View {
NavigationStack {
VStack {
ForEach(1..<12) { offset in
FooView(count: 12, offset: offset)
}
}
.padding()
.toolbar {
EditButton()
}
.environment(\.editMode, $mode)
}
}
}
struct FooView: View {
private static let colors: [Color] = [.red, .green, .blue, .yellow]
let count: Int
let offset: Int
var body: some View {
ZStack {
HStack {
ForEach(Array(0..<count), id: \.self) { i in
BarView(color: Self.colors[(i + offset) % Self.colors.count])
}
}
}
}
}
struct BarView: View {
@Environment(\.editMode) private var mode
let color: Color
var body: some View {
let mode = mode?.wrappedValue
Circle()
.foregroundStyle(mode == .active ? color : .gray)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment