Igor Muzyka
Igor Muzyka

Reputation: 409

Contributing animation for multi values change in SwiftUI(Animation Deprecation)

So, i have some structure which provides lots of parameters for me:

struct MyAppearance {

    public var offset: CGSize = .zero
    public var scale: CGFloat
    public var rotation: SwiftUI.Angle
    public var xAngle: CGFloat
    public var yAngle: CGFloat
    public var zAngle: CGFloat
    public var blur: CGFloat
    public var opacity: Double
    public var animation: SwiftUI.Animation?
}

public extension MyAppearance {

    func apply<Target: SwiftUI.View>(to view: Target) -> some SwiftUI.View {
        view
            .scaleEffect(scale)
            .rotation3DEffect(rotation, axis: (x: xAngle, y: yAngle, z: zAngle))
            .blur(radius: blur)
            .opacity(opacity)
            .offset(offset)
            .animation(animation) // this is the effected line
    }
}

It doesn't really make sense to have a @State as recommended, cause this whole structure is used to apply it's animation to all of the changes happening all at once. Deprecation notice warning is annoying though, how do i get rid of it in this case?

Upvotes: 2

Views: 568

Answers (1)

swiftPunk
swiftPunk

Reputation: 1

As you mentioned you do not need to use State wrapper for your function, but when you are going make use of it, you have to, here is an example:

Notice: if your value types are same you can mention them in an array, but if their types are deferent, then you need to apply one by one.

struct ContentView: View {

    @State private var myAppearance: MyAppearance = MyAppearance()
    
    var body: some View {

        VStack(spacing: 10.0) {
            
            Spacer()
  
            Text("Hello, world!")
                .apply(myAppearance: myAppearance)
            
            Spacer()
            
            Button("update scale") { myAppearance.scale *= 2.0 }
            Button("update blur") { updateBlur() }
            Button("update scale and blur") { myAppearance.scale *= 2.0; updateBlur() }
            Button("reset") { myAppearance.scale = 1.0; myAppearance.blur = 0.0 }
        }
        .padding()
  
    }
    
    private func updateBlur() {
        if (myAppearance.blur == .zero) { myAppearance.blur = 1.0 } else { myAppearance.blur *= 2.0 }
    }
}

extension View {

    func apply(myAppearance: MyAppearance) -> some View {
        return self
            .scaleEffect(myAppearance.scale)
            .blur(radius: myAppearance.blur)
            .scaleEffect(myAppearance.scale)
            .rotation3DEffect(myAppearance.rotation, axis: (x: myAppearance.xAngle, y: myAppearance.yAngle, z: myAppearance.zAngle))
            .blur(radius: myAppearance.blur)
            .opacity(myAppearance.opacity)
            .offset(myAppearance.offset)
            .animation(myAppearance.animation, value: myAppearance.offset)
            .animation(myAppearance.animation, value: [myAppearance.scale, myAppearance.xAngle, myAppearance.yAngle, myAppearance.zAngle, myAppearance.blur])
            .animation(myAppearance.animation, value: myAppearance.rotation)
            .animation(myAppearance.animation, value: myAppearance.opacity)
    }
}


struct MyAppearance {
    
    public var offset: CGSize
    public var scale: CGFloat
    public var rotation: Angle
    public var xAngle: CGFloat
    public var yAngle: CGFloat
    public var zAngle: CGFloat
    public var blur: CGFloat
    public var opacity: Double
    public var animation: Animation?
    
    init() {
        self.offset = .zero
        self.scale = 1.0
        self.rotation = Angle(degrees: .zero)
        self.xAngle = .zero
        self.yAngle = .zero
        self.zAngle = .zero
        self.blur = .zero
        self.opacity = 1.0
        self.animation = Animation.default
    }
    
    init(offset: CGSize, scale: CGFloat, rotation: Angle, xAngle: CGFloat, yAngle: CGFloat, zAngle: CGFloat, blur: CGFloat, opacity: Double, animation: Animation? = nil) {
        self.offset = offset
        self.scale = scale
        self.rotation = rotation
        self.xAngle = xAngle
        self.yAngle = yAngle
        self.zAngle = zAngle
        self.blur = blur
        self.opacity = opacity
        self.animation = animation
    }
}

Upvotes: 2

Related Questions