Matthew
Matthew

Reputation: 21

Animating SwiftUI view element makes the entire screen animate

I'm trying to navigate a single image inside a VStack. For some reason, this causes the entire VStack to move in and out from the left edge of the screen (easily seen if you add a color background to the VStack). The image itself does scale bigger and smaller as expected, but I don't understand why the whole screen moves.

The issue

Here is my code:

`struct ContentView: View { @State var scale = 0.1

var body: some View
{
    NavigationStack
    {
        VStack
        {
            Text("")
                .frame(width: getScreenBounds().width * 0.80, height: getScreenBounds().height * 0.03)
            
            Image("CVT1")
                .resizable()
                .scaledToFill()
                .frame(width: getScreenBounds().width * 0.80, height: getScreenBounds().height * 0.05)
                .scaleEffect(scale)
                .onAppear
                {
                    let baseAnimation = Animation.smooth(duration: 3)
                    let repeated = baseAnimation.repeatCount(3)
                    
                    withAnimation(repeated)
                    {
                        scale = 1.0
                    }
                }
        }
    }
}

}`

Upvotes: 0

Views: 48

Answers (1)

Matthew
Matthew

Reputation: 21

Sorry, figured it out:

           Image("CVT1")
                .resizable()
                .scaledToFill()
                .frame(width: getScreenBounds().width * 0.80, height: getScreenBounds().height * 0.05)
                .scaleEffect(scale)
                .onAppear
                {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.01)
                    {
                        let baseAnimation = Animation.smooth(duration: 1.5)
                        let repeated = baseAnimation.repeatCount(1)
                        
                        withAnimation(repeated)
                        {
                            scale = 1.0
                        }
                    }
                }

Upvotes: 1

Related Questions