Saturday, September 24, 2022
HomeiOS Developmentswift - Bizarre animation of a Button in Stack Views in iOS...

swift – Bizarre animation of a Button in Stack Views in iOS 16


I created a SampleView the place a Button is wrapped in a ZStack, and the entire ZStack is animated on faucet of the Button. It appears to be like like beneath:

enter image description here

struct SampleView: View {
    @State personal var toggle: Bool = false
    var physique: some View {
        ZStack {
            Button {
                toggle.toggle()
            } label: {
                Textual content("Faucet right here to maneuver!")
                    .font(.system(measurement: 20, weight: .black))
                    .foregroundColor(.black)
            }
            .padding(10)
            .background(.pink)
        }
        .offset(x: 0, y: toggle ? 0 : 200)
        .animation(.easeInOut(period: 1), worth: toggle)
    }
}

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        // ZStack {
            SampleView()
        // }
    }
}

There is a bunch of different views that I need to current, so I merely wrapped SampleView inside a VStack (or ZStack). Now, the animation began to interrupt:

enter image description here

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        ZStack {
            SampleView()
        }
    }
}

I seen that I can work round this conduct by wrapping the button motion with withAnimation.

withAnimation(.easeInOut(period: 1)) {
    toggle.toggle()
}

Nonetheless, I ponder what is going on on right here. Is that this a bug?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments