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

Unwanted position animation in iOS 14 SwiftUI

Forums > SwiftUI

Swift UI animations seem to behave differently regarding to the animation of a View position in its parent frame for iOS 14 versus iOS 13. My goal in the code snippet below is to only animate the resizing of the button text, which should happen on tapping it. Instead, in iOS 14, the displacement of the button, which is induced by the toggled visibility of an additional View in the VStack, is also being animated.

Identical code produces different outputs in iOS 13 (Xcode 11) and iOS 14 (Xcode 12 beta 6). In iOS 14, how can the old behavior be reproduced?

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    @State var toggle = false
    var body: some View {
        VStack{
            if self.toggle {
                Rectangle().frame(width: 200, height: 200)
            }
            Button(action: {
                self.toggle.toggle()
            }){
                Text("Tap me!")
                    .scaleEffect(self.toggle ? 2 : 1)
                    .animation(
                        Animation.easeInOut(duration: 1.5)
                    )
            }
        }
        .frame(width: 400, height: 400)
    }
}

PlaygroundPage.current.setLiveView(ContentView())

(see https://stackoverflow.com/questions/63766401/unwanted-position-animation-in-ios-14-swiftui)

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.