title: SwiftUI - Animate a view on button press
body: <p>I have just finished lesson 6 of #hackingwithswift. I'm going mad over a challenge that shouldn't be that hard at all...</p>
  <p>Here I have a working game called GuessTheFlag wherein the user must select the correct flag indicated then receives an alert before being passed to the next round.</p>
  <p>My problem is simple: I would like to have the incorrect flag views become opaque and have the correct flag view rotate 360 degrees when an answer is chosen. This should all happen behind the alert until the user clicks 'continue'.</p>
  <p>Optional: I would also like to remove the alert altogether and have the game move on to the next round after the rotating animation has finished. Can I bind the round change to the end of the animation? Or do I have to use some sort of sleep() command?</p>
  <pre><code>import SwiftUI
  
  struct FlagView: View {
      var flag: String
      
      var body: some View {
          Image(flag)
              .renderingMode(.original)
              .clipShape(Capsule())
              .overlay(Capsule().stroke(Color.black, lineWidth: 2))
              .shadow(color: .black, radius: 2)
      }
      
  }
  
  struct ContentView: View {
      @State private var countries = [&quot;Estonia&quot;, &quot;France&quot;, &quot;Germany&quot;, &quot;Ireland&quot;, &quot;Italy&quot;, &quot;Nigeria&quot;, &quot;Poland&quot;, &quot;Russia&quot;, &quot;Spain&quot;, &quot;UK&quot;, &quot;US&quot;].shuffled()
      
      @State private var correctAnswer = Int.random(in: 0...2)
      @State private var showingScore = false
      @State private var scoreTitle = &quot;&quot;
      @State private var score = 0
      @State private var animate = false
      
      var alert: Alert {
          if scoreTitle == &quot;Wrong&quot; {
              return Alert(title: Text(scoreTitle), message: Text(&quot;The correct answer was \(countries[correctAnswer])&quot;), dismissButton: .default(Text(&quot;Restart&quot;)) {
                  self.askQuestion()
                  animate = false
              })
          }
          else {
              return Alert(title: Text(scoreTitle), message: Text(&quot;Your Score is \(score)&quot;), dismissButton: .default(Text(&quot;Continue&quot;)) {
                  self.askQuestion()
                  animate = false
              })
          }
          
      }
  
      var body: some View {
          ZStack{
              LinearGradient(gradient: Gradient(colors: [.black,.blue, .orange]), startPoint: .top, endPoint: .bottom).edgesIgnoringSafeArea(.all)
              
              VStack(spacing: 40) {
                  VStack{
                      Text(&quot;Tap the flag of&quot;)
                          .foregroundColor(.white)
                      Text(countries[correctAnswer])
                          .font(.largeTitle)
                          .fontWeight(.black)
                          .foregroundColor(.white)
                  }
                  
                  ForEach(0 ..&lt; 3){ number in
                      Button(action: {
                          withAnimation {
                              self.flagTapped(number)
                              animate.toggle()
                          }
                      }) {
                          FlagView(flag: self.countries[number])
                      }
                  }
                  
                  Text(&quot;Your Score is \(score)&quot;)
                      .font(.headline)
                      .fontWeight(.black)
                      .foregroundColor(Color.white)
                  
                  Spacer()
              }
          }
          .alert(isPresented: $showingScore) {
              alert
          }
      }
  
      func flagTapped(_ number: Int) {
          if number == correctAnswer {
              scoreTitle = &quot;Correct&quot;
              score += 1
              
          } else {
              scoreTitle = &quot;Wrong&quot;
              score = 0
              
          }
          
          showingScore = true
          
      }
      
      func askQuestion() {
          countries.shuffle()
          correctAnswer = Int.random(in: 0...2)
      }
  }
  
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>
  <p>Any help would be appreciated!
  Thanks in advance</p>