WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.