UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.