‘animation’ was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.!
Welcome to the community!
Next time post a title for your question not “please help me with this” because that’s the point of this community, and it makes it easier for others to search if they have the same issue
What’s show in the screenshot it a warning, meaning you don’t need to do anything immediately now, but it’s warning you, you may need to change your code later.
The .animation
modifier will be depreciated (not used anymore) and will be replaced with withAnimation
or animation(_:value)
I have the same question.
We get that it’s been depreciated (not used anymore) and will be replaced with withAnimation
or animation(_:value)
, however what would be helpful is an example of how to use withAnimation
or animation(_:value)
and not receive the warning.
The code you have is basically OK except that to stop the complier having a cow, you need to link the animation to the trigger which in this case is the change in the @State variable rotate
.
By adding the value:
parameter in your .animation(........)
modifier to link the trigger you can solve that as follows:
.animation(Animation.easeInOut(duration: 5).repeatForever(autoreverses: false), value: rotate)
A simplified version of the above is that you don’t need to specify Animation.easeInOut
rather you can use the dot notation shortcut format by just saying .easeInOut
as follows:
.animation(.easeInOut(duration: 5).repeatForever(autoreverses: false), value: rotate)
So your overall code could be something like this:
struct LogoView: View {
@State private var rotate = false
var body: some View {
ZStack {
Image(systemName: "house")
.resizable()
.frame(width: 150, height: 150)
.shadow(color: .gray, radius: 5, x: 0, y: 0)
.rotationEffect(.degrees(rotate ? 360: 0))
.animation(.easeInOut(duration: 5).repeatForever(autoreverses: false), value: rotate)
.onTapGesture {
rotate = true
}
}
}
}
The other alternative is to use the withAnimation
clause linked with the code triggering the animation like this:
struct LogoView2: View {
@State private var rotate = false
var body: some View {
ZStack {
Image(systemName: "house")
.resizable()
.frame(width: 150, height: 150)
.shadow(color: .gray, radius: 5, x: 0, y: 0)
.rotationEffect(.degrees(rotate ? 360: 0))
.onTapGesture {
withAnimation(.easeInOut(duration: 5).repeatForever(autoreverses: false)) {
rotate = true
}
}
}
}
}
This achieves exactly the same result but the linkage with the change in state is inside the tap gesture withAnimation
closure where rotate
is changed to true
Xcode never provides an alternative example usage where a deprecation occurs. What the complier will tell you is that such and such has been deprecated and suggests what to use in place of but what you have to do is then consult the Apple documentation for that suggested alternative. As you will see the Apple documentation for withAnimation
is next to absolutely useless (at least that’s my opinion) so as it is often the case, we end up searching on line for an example use case and see if someone knows how to use it. Paul Hudson (hackingwithswift.com) is a useful resource as is stackoverflow.com
I searched for “SwiftUI withAnimation example”
Hope that helps.
Thanks for the help.