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

Why does one rectangle grow up, while the other grows down...

Forums > SwiftUI

@speg  


import SwiftUI

struct GrowView: View {
    var size:CGFloat = 400
    @State var scale:CGFloat = 0
    var body: some View {
        HStack(alignment: .bottom) {
            Rectangle()
                .foregroundColor(.red)
                .frame(height: 300 * scale)
                .animation(.default)
            if true {
                Rectangle()
                    .foregroundColor(.red)
                    .frame(height: 400 * scale)
                    .animation(.default)
            }

        }.onAppear {
            self.scale = 1
        }.frame( maxHeight: 500, alignment: .bottom)
    }
}

struct GrowView_Previews: PreviewProvider {
    static var previews: some View {
        GrowView()
    }
}

Without the if statement, both rectangles animate from the bottom up. With the if block around the second one, it appears to animate from the top down 🤔

I will be eventually adding a delay to the second rectangle.

2      

@speg  

Update: If I remove the if statement around the second Rectangle they both grow from the bottom. Not what I was expecting...

2      

Good question... I did a little playing around and added .onAppear modifiers to both rectangles. I printed "1" for the first rectangle and "2" for the second rectangle. When i used the if statement the debug output was as follows:

2 1 2 1

So from this we can see that the first 2 1 is printed when the view is about to appear. Interestingly it looks like it looks at any conditional statements first and applies it before any other views are used. In this case it looks at the if statement first then the other rectangly with no if statement. On the second 2 1 is when the view actually appears. It again looks at the if statement first sets this up and then does the second.

When i took away the if statement the debug output was as follows - 1 2 1 2

So here it just looks at each view as it goes down through the code. Whether this has anything to do with how the views animate im not sure i just found it interesting and it may have something to do with it since they are both in the same HStack. When you look at it if the condition for the if statement was false then the first rectangle would take up the whole HStack and come up from the bottom. So it could have something to do with layout and sizing within the HStack as well.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.