Thursday, October 29, 2026
HomeiOS DevelopmentSwiftUI: 'worth' parameter not working as anticipated in iOS 15's new animation...

SwiftUI: ‘worth’ parameter not working as anticipated in iOS 15’s new animation methodology signature

[ad_1]

I am attempting to unravel the iOS 15’s warning:
'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:worth:) as an alternative. in a view that seems on prime of display screen that appears like push notification alert (or what so referred to as snack bar), however the view loses animation impact once I use any Equatable worth within the worth parameter, the code for this view is as follows (you’ll be able to copy & paste it as is to attempt it out):

import Basis
import SwiftUI

struct ContentView: View {
    @State non-public var showSnackBar = false
    
    var physique: some View {
        
        ZStack {
            Colour.grey
            
            Textual content("Present Me")
                .padding()
                .onTapGesture {
                    showSnackBar = true
                }
            
            EmptyView()
                .snackBar(title: "this can be a check message", present: $showSnackBar)
                .offset(x: 0, y:  50)
        }
        .edgesIgnoringSafeArea(.all)
    }
}


struct SnackBarModifier: ViewModifier {
    var title: String
    @Binding var present: Bool
    @State var activity: DispatchWorkItem?
    
    func physique(content material: Content material) -> some View {
        
        ZStack {
            if self.present {
                VStack {
                    HStack {
                        
                        Textual content(title)
                        
                        Spacer()
                    }
                    .foregroundColor(.white)
                    .padding(12)
                    .background(Colour.orange)
                    .cornerRadius(8)
                    
                    Spacer()
                }
                    .body(maxWidth: UIScreen.essential.bounds.width - 40)
                    .animation(.easeInOut(length: 0.7), worth: ...) //<---- that is the place I ought to use the worth with a purpose to resolve iOS 15 animation deprecation warning
                    .transition(AnyTransition.transfer(edge: .prime)
                        .mixed(with: .opacity))
                    
                    .onTapGesture {
                        withAnimation {
                            self.present = false
                        }
                }
                .onAppear {
                    self.activity = DispatchWorkItem {
                        withAnimation {
                            self.present = false
                        }
                    }
                    // Auto dismiss after 2 seconds
                    DispatchQueue.essential.asyncAfter(deadline: .now() + 2, execute: self.activity!)
                }
                .onDisappear {
                    self.activity?.cancel()
                }
            }
            
            content material
        }
        
    }
}

extension View {
    func snackBar(title: String, present: Binding<Bool>) -> some View {
        self.modifier(SnackBarModifier(title: title, present: present))
    }
}

I attempted to make use of a separate boolean state var that’s toggled when the self.present is ready to true however to no avail, how can I animate the looks of this snack bar view utilizing easeInOut(length:) animation utilizing iOS 15’s animation model ?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments