TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 33 (Animation): Showing and hiding views with transitions

Forums > 100 Days of SwiftUI

The tutorial says this code should animate the button by moving it up and down, and the rectangle by scaling it in and out, but I'm seeing something different. The button does animate up and down, but the red rectangle only animates out. It just appears abruptly without animation. Any ideas?

struct ContentView: View {
    @State private var isShowingRed = false

    var body: some View {
        VStack {
            Button("Tap Me") {
                withAnimation {
                    self.isShowingRed.toggle()
                }
            }

            if isShowingRed {
                Rectangle()
                    .fill(Color.red)
                    .frame(width: 200, height: 200)
                    .transition(.scale)
            }
        }
    }
}

3      

Hi Joshua,

I just tried this code and it appears to work in the simulator but not the SwiftUI preview screen. Could be a bug, have you tried it on your iPhone?

Hope this helps

PS. I sometimes find things don't work in preview or even in the simulator, but work fine on an actual device.

4      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.