.animation Modifier

Hello Everyone,
I’m currently reading Chapter 11: Gestures.
.animation(_ animation:) modifier is deprecated and in iOS 15 there is a required parameter “value” where, according to apple documentation, “value” is “A value to monitor for changes.”
In our case what value are we monitoring?

import SwiftUI

struct CardView: View {
    var body: some View {
        ZStack {
            Rectangle()
                .fill(Color.red)
                .frame(width: 320, height: 210)
                .cornerRadius(12)
            VStack {
                Spacer()
                Text("Apple")
                    .font(.largeTitle)
                    .foregroundColor(.white)
                Text("Omena")
                    .font(.caption)
                    .foregroundColor(.white)
                Spacer()
            }
        }
        .shadow(radius: 8)
        .frame(width: 320, height: 210)
        .animation(.spring()) // * yellow warning: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.
       .animation(.spring(), value: ??)
    }
}

struct CardView_Previews: PreviewProvider {
    static var previews: some View {
        CardView()
    }
}

Hi @gaspare92

Although you can use the deprecated API, if you want to use the new version you can temporarily set value to a literal, such as 0. Later on, in the “Custom gestures” section, you’ll add a offset property to CardView, and at that point you can replace that literal with self.offset.

The current version of the book (i.e. the one you’re reading) is for SwiftUI 2.0 and iOS 14 — an update is in the works, and, among a lot of other updates, this specific issue has been taken care of.

1 Like

I know but yellow warning annoy me. :rofl:

Yeah it’s better to keep them under control, otherwise the risk is that they grow exponentially and you learn to just ignore that icon… :scream:

1 Like