NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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.
    }
}

1      

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?

2      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.