小编Sha*_*tis的帖子

如何使用 SwiftUI 制作明暗模式之间的过渡动​​画?

我不知道 SwiftUI 是否支持此功能,但我正在尝试在按钮为 a 时在应用程序范围内设置从浅色/深色模式的过渡动画。我的屏幕底部有一个导航栏,其中有一个按钮,点击时可以从太阳图标切换到月亮图标。

从浅色模式到深色模式的切换确实有效,反之亦然,但是是立即的,看起来就像有人刚刚关掉了灯。以下代码中使用的动画仅用动画更改我的按钮的图像。

这是我所拥有的:

导航栏视图.swift

struct NavigationBarView: View {

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

var body: some View {
    ZStack {
        ...
            Button(action: {
                withAnimation(.easeInOut) {
                    isDarkMode.toggle()
                }
            }, label: {
                if isDarkMode {
                    Image.sunButton
                        .resizable()
                        .imageScale(.large)
                        .frame(width: 24, height: 24)
                        .foregroundColor(Color.darkGray)
                } else {
                    Image.moonButton
                        .resizable()
                        .imageScale(.large)
                        .frame(width: 24, height: 24)
                        .foregroundColor(Color.darkGray)
                }
                
            })
            
        ...
        
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

}

LucidityApp.swift

@main
struct LucidityApp: App {

@Environment(\.colorScheme) var colorScheme
@AppStorage("isDarkMode") private var isDarkMode = …
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

6
推荐指数
1
解决办法
1105
查看次数

标签 统计

ios ×1

swift ×1

swiftui ×1