Home > other >  How to replace deprecated .animation() in SwiftUI?
How to replace deprecated .animation() in SwiftUI?

Time:10-05

The .animation() modifier has been deprecated in iOS 15, but I'm not sure I understand how Xcode's suggested equivalent, animation(_:value:), works.

.animation(.easeInOut(duration: 2)) // ⚠️'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.

How would I change my code to get rid of the warning?

CodePudding user response:

You need tell Xcode what exactly should be animated! With given a variable that conform to Equatable protocol. it could be State or Binding or any other wrapper that allow you to update the value of it.

Example:

 struct ContentView: View {

    @State private var offset: CGFloat = .zero
    
    var body: some View {

        Circle()
            .frame(width: 100, height: 100, alignment: .center)
            .offset(x: 0, y: offset)
            .onTapGesture {
                offset  = 100.0
            }
            .animation(.default, value: offset)
        
    }
    
}

CodePudding user response:

.animation now takes a second argument called value.

https://developer.apple.com/documentation/swiftui/view/animation(_:value:)

  • Related