ios – How to animate the transition between light and dark mode using SwiftUI?


I have no idea if that’s supported however with SwiftUI, nevertheless I’m trying to animate the transition from Light/Darkish mode app-wide when the button is a. I’ve a navigation bar on the underside of the show with a button that switches from a photo voltaic to moon icon when tapped.

The swap from light mode to darkish mode and vice-versa does work, nevertheless is fast and seems like any person merely flipped the lights off. The animation use throughout the following code solely changes the image of my button with animation.

Right here’s what I’ve:

NavigationBarView.swift

struct NavigationBarView: View {

@AppStorage("isDarkMode") personal var isDarkMode = false
...

var physique: some View {
    ZStack {
        ...
            Button(movement: {
                withAnimation(.easeInOut) {
                    isDarkMode.toggle()
                }
            }, label: {
                if isDarkMode {
                    Image.sunButton
                        .resizable()
                        .imageScale(.big)
                        .physique(width: 24, peak: 24)
                        .foregroundColor(Coloration.darkGray)
                } else {
                    Image.moonButton
                        .resizable()
                        .imageScale(.big)
                        .physique(width: 24, peak: 24)
                        .foregroundColor(Coloration.darkGray)
                }
                
            })
            
        ...
        
        }
    }
    ...
}

}

LucidityApp.swift

@important
struct LucidityApp: App {

@Ambiance(.colorScheme) var colorScheme
@AppStorage("isDarkMode") personal var isDarkMode = false

var physique: some Scene {
    WindowGroup {
        Dashboard()
            .preferredColorScheme(isDarkMode ? .darkish : .light)
            .background(colorScheme == .darkish ? Coloration.darkGray : Coloration.white)
    }
}

}

Thanks prematurely for the help, and I’m new to SwiftUI so if in case you’ve got any most interesting comply with(s) concepts, I’ll gladly take heed to them!

Shane



Supply hyperlink

Leave a Reply

Your email address will not be published.