1

ok, I'm stumped with swiftui animations. I get the basics (I think) but how does one get a view to animate / transition in from the absolute bottom of the screen into it's final position - I can't seem to get this ... I've experimented with offset and tried changing layouts about with Spacer() but the solution is eluding me

struct CardView: View {
let card: Card
var body: some View{

    VStack { 
        Spacer()
            Text("\(card.factor1) X \(card.factor2)")
                .font(.title)
                .foregroundColor(.black)

                .multilineTextAlignment(.center)
                .padding(.horizontal)
                Spacer()
    }
    .padding()
    .frame(width: 300, height: 200)
    .background(
      Rectangle()
           .foregroundColor(cardColor(of: card))
    )

}

func cardColor(of card: Card) -> Color {
    return Color(red:228/255, green: 229/255, blue: 229/255)
}
}

struct AnswerView: View {
let card: Card
var body: some View {
    HStack {
        Spacer()
        ForEach(card.possibleAnswers, id:\.self) { answer in
            Text("\(answer)")
                .foregroundColor(.black)
                .frame(width: 75, height: 50)
                .background(
                  Rectangle()
                       .foregroundColor(cardColor(of: card))
                )
            Spacer()
        }
    }
}

func cardColor(of card: Card) -> Color {
    return Color(red:228/255, green: 229/255, blue: 229/255)
}
}

struct ContentView: View {
@StateObject var deck = Deck()
@State private var isActive = false

var cardActive = false

var body: some View {
    ZStack {
        VStack {
            Text("Total Cards: \(deck.count)")
            if isActive {

           if let card = deck.cards[0] {
                Spacer()
                CardView(card: card)
                  .transition(.move(edge: .bottom))
                    .animation(.easeInOut(duration: 1))
                    .padding()
              AnswerView(card: card)
                  .transition(.move(edge: .bottom))
                .animation(.easeInOut(duration: 1).delay(1))
                Spacer()
                    
            }

        } else {
            Button(action: {
                    isActive.toggle()
            }) {
            ZStack {
                    RoundedRectangle(cornerRadius: 25, style: .continuous)
                        .fill(Color.gray)
                        .opacity(0.5)
                    Text("Begin")
                        .foregroundColor(.black)
            }
            .frame(width: 225, height: 25)
            .padding(5)
            }
           }
    }
    }
    }
  }
  • Can you come up with a [mre] to start with? Right now, people would have to stub out `Card` and `Deck` to run your example. – jnpdx Jun 06 '21 at 04:23
  • 1
    It is not clear what do you try to achieve.... also I would not allow to think in terms of *absolute* positions as it might break layout at some of physical device (due to variety of form factors), instead layout should always be relative. – Asperi Jun 06 '21 at 06:41

0 Answers0