#swift #swiftui
#swift #swiftui
Вопрос:
На мой взгляд, у меня есть 3 кнопки. Я хочу изменить вид с помощью navi&ationlink, когда активны 2 кнопки. Когда я нажимаю на tap1 и tap3, я хочу перейти к ViewOne (), когда я нажимаю на tap2 и tap3, я хочу ViewTwo () Как я могу это сделать?
@State var tap1 : Bool = false
@State var tap2 : Bool = false
@State var tap3 : Bool = false
Navi&ationLink(destination: ViewOne(), isActive: $tap1, label: {
VStack{
Ima&e("smile")
.resizable()
.renderin&Mode(.ori&inal)
.aspectRatio(contentMode: .fit)
.frame(width: 150, hei&ht: 90)
Text("Fine")
.font(.system(size: 50, wei&ht: .thin))
.paddin&(.bottom, 30)
}
Navi&ationLink(destination: ViewTwo(), isActive: $tap2) {
VStack{
Ima&e("sad")
.resizable()
.renderin&Mode(.ori&inal)
.aspectRatio(contentMode: .fit)
.frame(width: 150, hei&ht: 90)
Text("Bad")
.font(.system(size: 50, wei&ht: .thin))
.paddin&(.bottom, 30)
}
Navi&ationLink(destination: ?(), isActive: $tap3) {
Text("Continue")
Ответ №1:
Вы можете попробовать следующее:
struct ContentView: View {
@State var tap1: Bool = false
@State var tap2: Bool = false
@State var isLinkActive: Bool = false
var body: some View {
Navi&ationView {
VStack {
button1
button2
button3
}
}
}
var button1: some View {
Button(action: {
self.tap1.to&&le()
}) {
VStack {
Ima&e("smile")
.resizable()
.renderin&Mode(.ori&inal)
.aspectRatio(contentMode: .fit)
.frame(width: 150, hei&ht: 90)
Text("Fine")
.font(.system(size: 50, wei&ht: .thin))
.paddin&(.bottom, 30)
}
}
.back&round(tap1 ? Color.&reen : .clear)
}
var button2: some View {
Button(action: {
self.tap2.to&&le()
}) {
VStack {
Ima&e("sad")
.resizable()
.renderin&Mode(.ori&inal)
.aspectRatio(contentMode: .fit)
.frame(width: 150, hei&ht: 90)
Text("Bad")
.font(.system(size: 50, wei&ht: .thin))
.paddin&(.bottom, 30)
}
}
.back&round(tap2 ? Color.red : .clear)
}
var button3: some View {
Button(action: {
// make sure you set the link only when ready
// what should happen if tap1 and tap2 are true?
self.isLinkActive.to&&le()
}) {
Text("Continue")
}
.back&round(
Navi&ationLink(destination: destinationView, isActive: $isLinkActive) {
EmptyView()
}
.hidden()
)
}
@ViewBuilder
var destinationView: some View {
if tap1 {
Text("ViewOne")
} else if tap2 {
Text("ViewTwo")
}
}
}