Your First iOS & SwiftUI App: Polishing the App, Episode 28: Intro to SwiftUI Animation | Kodeco, the new raywenderlich.com

Get a crash course in SwiftUI animation and use it to make the custom alert smoothly animate onto the screen.


This is a companion discussion topic for the original entry at https://www.kodeco.com/28797859-your-first-ios-swiftui-app-polishing-the-app/lessons/28

Wouldn’t it be enough to set the transition modifier only on the creation of the points view in this case, instead of also setting it within the PointsView struct itself?

ContentView.swift

if alertIsVisible {
                    PointsView(
                        alertIsVisible: $alertIsVisible,
                        sliderValue: $sliderValue,
                        game: $game
                    )
                    .transition(.scale) // WE ARE ALREADY SETTING THE TRANSITION HERE
                } else {
                    HitMeButton(alertIsVisible: $alertIsVisible, sliderValue: $sliderValue, game: $game)
                        .transition(.scale)
                }

PointView.swift

 VStack (spacing: 10) {
            InstructionText(text: "The Slider's value is".uppercased())
            BigNumberText(text: String(roundedValue))
            BodyText(text: "You scored \(points) Points\n🎉🎉🎉")
            Button {
                withAnimation {
                    alertIsVisible = false
                }
                
                game.startNewRound(points: points)
            } label: {
                ButtonText(text: "Start New Round")
            }
        }
        .padding()
        .frame(maxWidth: 300)
        .background(Color("BackgroundColor"))
        .cornerRadius(21)
        .shadow(radius: 10, x: 5, y: 5)
        .transition(.scale) // IF I'M RIGHT, THIS HERE IS NOT NECESSARY SINCE WE ALREADY SET IT IN CONTENTVIEW?

I’ve checked it and it seems to work exactly the same if I either remove it from PointsView or from ContentView and just use it in either of these places. That was also the behavior I expected but maybe I am missing something?

Thank you in advance!