how do i switch to only the first item in an array?

That is because your @State var back is for the array.

If you want to control each card you need a CardView with an @State var back for each card.

I put some sample code below. A minimum reproducible example is preferred so we don’t have to recreate struct‘s and other components

import SwiftUI

struct TryView: View {
    //@ObservedObject var cards = APIObs()//Code not Provided
    @State var cards = ["Alpha", "Beta", "Charlie"]
    var body: some View {
        
        GeometryReader{geo in
            VStack{
                ForEach(cards, id: \.self){ i in
                    CardView(card: i)
                }
                
            }
        }
    }
}
struct SwipeDetailsView1: View {
    var name: String
    var body: some View {
        Text(name)
    }
}
struct BackView: View {
    var name: String
    var body: some View {
        Text(name)
    }
}

struct CardView: View {
    @State var back = false //Variable for just the card
    var card: String
    var body: some View {
        VStack{
            ZStack{
                if back == false{
                    
                    SwipeDetailsView1(name: "front \(card)")
                    
                }else{
                    BackView(name: "back \(card)")
                    
                }
            }.animation(.easeOut(duration: 0.25))
            Button("flip", action: {
                back.toggle()
            })
        }
        
    }
}
struct TryView_Previews: PreviewProvider {
    static var previews: some View {
        TryView()
    }
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top