Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace deprecated .animation() in SwiftUI?

The .animation() modifier has been deprecated in iOS 15, but I'm not sure I understand how Xcode's suggested equivalent, animation(_:value:), works.

.animation(.easeInOut(duration: 2)) // ⚠️'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.

How would I change my code to get rid of the warning?

like image 464
Sam Avatar asked Oct 05 '21 00:10

Sam


People also ask

How do I animate other values in SwiftUI?

You can animate other values by making your custom views or view modifiers conform to the Animatable protocol and telling SwiftUI about the value you want to animate.

What are the benefits of using animatable in SwiftUI?

By conforming to Animatable, you can effectively decouple the animation of your view from the concept of duration, as you give SwiftUI the ability to interpolate arbitrarily between two different values for animatableData.

Is 'animation' deprecated in iOS 15?

'animation' was deprecated in iOS 15.0: Use withAnimation or animation (_:value:) instead. animation function without value parameter is marked to be deprecated in API: Value parameter provides a way to control when the animation should be played. Consider code below: Pay attention to animation modifier.


Video Answer


6 Answers

You need tell Xcode what exactly should be animated! With given a variable that conform to Equatable protocol. That could be State or Binding or any other wrapper that allow you to update the value of it.

Example:

struct ContentView: View {
    
    @State private var offset: CGFloat = 200.0
    
    var body: some View {
        
        Image(systemName: "ant")
            .font(Font.system(size: 100.0))
            .offset(y: offset)
            .shadow(radius: 10.0)
            .onTapGesture { offset -= 100.0 }
            .animation(Animation.easeInOut(duration: 1.0), value: offset)
        
    }
}

Result:

enter image description here

like image 96
swiftPunk Avatar answered Sep 28 '22 14:09

swiftPunk


You can also use the animation by using a boolean as the value.

Example Below: When you tap the CardView(), it will toggle show which in turn will animate the offset both ways.

@State var show = false

CardView()
 .offset(x: 0, y: show ? -100 : -10)
 .animation(.easeInOut(duration: 1.0), value: show)
 .onTapGesture {
   show.toggle()
 }
like image 34
STerrier Avatar answered Oct 20 '22 11:10

STerrier


Another approach would be to embed ontapGesture in a withAnimation

struct SwiftUWithAnimation: View {
@State private var offset: CGFloat = .zero

var body: some View {
    
    Circle()
        .frame(width: 100, height: 100, alignment: .center)
        .offset(x: 0, y: offset)
        .onTapGesture {
            withAnimation(.default) {
                offset += 100.0
         }
      }
   }
}
like image 24
L. Puzer Avatar answered Oct 20 '22 11:10

L. Puzer


.animation now takes a second argument called value.

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

like image 27
iPaat Avatar answered Oct 20 '22 09:10

iPaat


You need tell Xcode what exactly should be animated! With given a variable that conform to Equatable protocol. That could be State or Binding or any other wrapper that allow you to update the value of it.

Example:

struct ContentView: View {
    
    @State private var offset: CGFloat = 200.0
    
    var body: some View {
        
        Image(systemName: "ant")
            .font(Font.system(size: 100.0))
            .offset(y: offset)
            .shadow(radius: 10.0)
            .onTapGesture { offset -= 100.0 }
            .animation(Animation.easeInOut(duration: 1.0), value: offset)
        
    }
}

Result:

enter image description here

like image 45
ios coder Avatar answered Oct 20 '22 09:10

ios coder


UUID


extension View {
    func withoutAnimation() -> some View {
        self.animation(nil, value: UUID())
    }
}


demo

/*

.animation(
  Animation
    .easeInOut(duration: 1.5)
    .repeatForever(autoreverses: true)
)

*/

// ⚠️ 'animation' was deprecated in iOS 15.0,
// Use withAnimation or animation(_:value:) instead.


// value: UUID ✅
.animation(
  Animation
    .easeInOut(duration: 1.5)
    .repeatForever(autoreverses: true), 
  value: UUID()
)


refs

https://developer.apple.com/forums/thread/688947

like image 1
xgqfrms Avatar answered Oct 20 '22 11:10

xgqfrms