gpt4 book ai didi

ios - 如何使用dynamicProvider为暗模式创建初始化程序

转载 作者:行者123 更新时间:2023-11-29 05:23:47 25 4
gpt4 key购买 nike

致力于支持深色模式,我已经能够使用这种模式更改主题时动态颜色的变化:

static var dynamicColor: UIColor = {
if #available(iOS 13, *) {
return UIColor { (traitCollection: UITraitCollection) -> UIColor in
switch (traitCollection.userInterfaceStyle, traitCollection.accessibilityContrast) {
case (.dark, .high):
return .somethingCustom4

case (.dark, _):
return .somethingCustom2

case (_, .high):
return .somethingCustom3

default:
return .somethingCustom1
}
}

} else {
return .somethingCustom1
}
}()

我正在尝试简化 iOS 12 和 13 之间 UIColor 的创建,并支持深色模式。我想出了一个简单的 Theme 枚举来封装上面的所有逻辑,它只公开了 4 个案例,因此上面的声明简化为:

static var customColor: UIColor = {
switch UIColor.theme {
case .light:
return somethingCustom1

case .dark:
return .somethingCustom2

case .lightHighContrast:
return somethingCustom3

case .darkHighContrast:
return .somethingCustom4
}
}()

问题是,虽然在简化之前所有内容都按应有的方式更新,但现在主题没有获取更新的值,因此颜色不会改变。这就是我使用 init(dynamicProvider:) 初始值设定项声明 Theme 的方式,但似乎 UITraitCollection.current 没有在暗/亮模式之间切换时的正确值:

public enum Theme {
case light, dark, lightHighContrast, darkHighContrast

@available(iOSApplicationExtension 13.0, *)
init(dynamicProvider: @escaping (UITraitCollection) -> Theme) {
self = dynamicProvider(UITraitCollection.current)
}
}

public extension UIColor {
static var theme: Theme {
if #available(iOS 13, *) {
return Theme { (traitCollection: UITraitCollection) -> Theme in
switch (traitCollection.userInterfaceStyle, traitCollection.accessibilityContrast) {
case (.dark, .high):
return .darkHighContrast

case (.dark, _):
return .dark

case (_, .high):
return .lightHighContrast

default:
return .light
}
}

} else {
return .light
}
}
}

我在这里做错了什么?我认为初始化程序应该是这样工作的,但我无法在网上找到任何其他示例......

最佳答案

好吧,经过一番折腾后,我仍然不确定为什么最初的实现不起作用,但我确实找到了另一种让它起作用的方法!因此,如果您想简化实现以支持 iOS 12 和 13,我最终得到的结果如下:

public extension UIColor {
private static func make(dynamicProvider: @escaping (Theme) -> UIColor) -> UIColor {
guard #available(iOSApplicationExtension 13.0, *) else { return dynamicProvider(.light) }
return UIColor { (traitCollection) -> UIColor in
return dynamicProvider(Theme(traitCollection))
}
}

static var customColor: UIColor {
return .make { (theme) -> UIColor in
switch theme {
case .light:
return .somethingCustom1

case .dark:
return .somethingCustom2

case .lightHighContrast:
return .somethingCustom3

case .darkHighContrast:
return .somethingCustom4
}
}
}
}

public enum Theme {
case light, dark, lightHighContrast, darkHighContrast

@available(iOSApplicationExtension 13.0, *)
init(_ traitCollection: UITraitCollection) {
switch (traitCollection.userInterfaceStyle, traitCollection.accessibilityContrast) {
case (.dark, .high):
self = .darkHighContrast

case (.dark, _):
self = .dark

case (_, .high):
self = .lightHighContrast

default:
self = .light
}
}
}

关于ios - 如何使用dynamicProvider为暗模式创建初始化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com