Created
May 10, 2024 21:44
-
-
Save Koshimizu-Takehito/98abbbc3da709de9f23104abf0a1676b to your computer and use it in GitHub Desktop.
EditMode Sample
This file contains 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 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