为了控制我的应用程序中的所有颜色,我创建了一个包含静态颜色变量的枚举。
enum Color {
static var mainColor = UIColor(named: "mainColor")
static let red = UIColor(named: "red")
static let blue = UIColor(named: "blue")
static let green = UIColor(named: "green")
static let magenta = UIColor(named: "magenta")
static let indigo = UIColor(named: "indigo")
static let orange = UIColor(named: "orange")
static let pink = UIColor(named: "pink")
static let purple = UIColor(named: "purple")
static let yellow = UIColor(named: "yellow")
static let white = UIColor(named: "white")
}
我在我的应用中到处都使用 mainColor 作为背景。但是在我的 SettingsView 中,有这些颜色的集合,当您点击其中一个时,我想将 mainColor 更改为它。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == settingsView.primary.collection{
let color = collectionView.cellForItem(at: indexPath)?.backgroundColor
Color.mainColor = color
}
}
我该怎么做?