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

SOLVED: Hacking with watchOS: SwiftUI Edition - Project 9 Animation

Forums > Books

I got the following warning while working on Project 9. How do I implement the suggested functions instead of the deprecated modifier? Thanks Jake

import SwiftUI

struct ContentView: View {
@State private var animationAmount: CGFloat = 1

var body: some View {
Button("Tap Me") {
    // do nothing
}
.buttonStyle(PlainButtonStyle())
.padding(50)
.background(Color.red)
.foregroundColor(.white)
.clipShape(Circle())
.scaleEffect(animationAmount)
.animation(.default)           'animation' was deprecated in watchOS 8.0: Use withAnimation or animation(:value:) instead.
    }
}

4      

Let's take a quick look at the updated animation documentation.

Link to Apple's documentation.

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

Here's a snip: Applies the given animation to this view when the specified value changes.

The old definition just asked you to supply the type of animation to apply. (.linear, .default, .easeIn, etc.)

The new definition requires an additional parameter, a value. What kind of value? The method says it will take any type of value (noted as a generic V) as long as that value conforms to the Equatable protocol.

More importantly, the method states that this particular animation will be applied when that value changes.

You may have many vars in your struct. This definition will apply the animation only when the var you supply is changed. Superb!

So getting back to the meat of your question.... what variable in your struct is changing? More directly, you want some animation to occur when which value changes?

5      

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.