通过按下按钮在两种颜色之间交替 UIView 背景
Alternate UIView background between two colors by pressing a button
我正在尝试设置一个按钮,每次按下该按钮都会在两种颜色(黑色和白色)之间交替。
最初,应用程序以黑色背景加载,第一次点击按钮切换到白色背景,第二次点击切换回黑色背景,等等。
我想这将是一个简单的 if else 语句,但是我不知道如何创建一个变量作为当前 UI 背景颜色。
@IBAction func background_toggle(_ sender: UIButton) {
self.view.backgroundColor = UIColor.white
}
按钮将其切换为白色,但没有任何实现可以在第二次按下时切换回黑色。
您可以使用backgroundColor
作为条件来检查您接下来应该选择哪一个。
使用 backgroundColor 属性的方法
@IBAction func background_toggle(_ sender: UIButton) {
if self.view.backgroundColor == .white {
self.view.backgroundColor = .black
} else {
self.view.backgroundColor = .white
}
}
换句话说:如果backgroundColor
是.white
,那么就设置.black
否则设置.white
提示:
因为 backgroundColor
是一个 UIColor
,Swift 推断出类型,所以你可以在赋值的时候直接输入 .white
或 .black
,不用指定 UIColor.white
或 UIColor.black
.
您可以有一个状态变量,例如 Bool
、UIColor
或 Enum
。然后你比较那个而不是视图的backgroundColor
。
使用布尔变量的方法
var isWhite = true
@objc func handleOpenTermsAndConditions() {
if self.isWhite {
self.isWhite = false
self.view.backgroundColor = .black
} else {
self.isWhite = true
self.view.backgroundColor = .white
}
}
使用 UIColor 变量的方法
var currentBackgroundColor : UIColor = .white
@objc func handleOpenTermsAndConditions() {
if currentBackgroundColor == .white {
self.currentBackgroundColor = .black
self.view.backgroundColor = .black
} else {
self.currentBackgroundColor = .white
self.view.backgroundColor = .white
}
}
使用枚举状态的方法
如果您想限制颜色,您可以创建具有可用颜色的 Enum
。我认为将它与一些像 .white
| 这样简单的逻辑一起使用有点过分了。 .black
.
第 1 步:创建一个 enum
。
enum AvailableColor {
case white
case black
func currentUIColor() -> UIColor {
switch self {
case .white:
return UIColor.white
case .black:
return UIColor.black
}
}
}
第 2 步:实例化状态变量并使用 enum
逻辑
var currentColorState : AvailableColor = .white
@objc func handleOpenTermsAndConditions() {
if currentColorState == .white {
currentColorState = .black
} else {
currentColorState = .white
}
self.view.backgroundColor = currentColorState.currentUIColor()
}
不要试图通过比较颜色来使用视图的内容来确定下一个状态。
将 model 与 view 分开,并保留一个具有当前状态的单独 属性。您可以使用一个简单的布尔标志 isBlack
,或者您可以创建一个枚举:
enum BackgroundState {
case black, white
}
如果您想要另一个 color/state,它有一个更容易扩展的小优势。 (CaseIterable
可能也会有帮助。)
我正在尝试设置一个按钮,每次按下该按钮都会在两种颜色(黑色和白色)之间交替。 最初,应用程序以黑色背景加载,第一次点击按钮切换到白色背景,第二次点击切换回黑色背景,等等。
我想这将是一个简单的 if else 语句,但是我不知道如何创建一个变量作为当前 UI 背景颜色。
@IBAction func background_toggle(_ sender: UIButton) {
self.view.backgroundColor = UIColor.white
}
按钮将其切换为白色,但没有任何实现可以在第二次按下时切换回黑色。
您可以使用backgroundColor
作为条件来检查您接下来应该选择哪一个。
使用 backgroundColor 属性的方法
@IBAction func background_toggle(_ sender: UIButton) {
if self.view.backgroundColor == .white {
self.view.backgroundColor = .black
} else {
self.view.backgroundColor = .white
}
}
换句话说:如果backgroundColor
是.white
,那么就设置.black
否则设置.white
提示:
因为 backgroundColor
是一个 UIColor
,Swift 推断出类型,所以你可以在赋值的时候直接输入 .white
或 .black
,不用指定 UIColor.white
或 UIColor.black
.
您可以有一个状态变量,例如 Bool
、UIColor
或 Enum
。然后你比较那个而不是视图的backgroundColor
。
使用布尔变量的方法
var isWhite = true
@objc func handleOpenTermsAndConditions() {
if self.isWhite {
self.isWhite = false
self.view.backgroundColor = .black
} else {
self.isWhite = true
self.view.backgroundColor = .white
}
}
使用 UIColor 变量的方法
var currentBackgroundColor : UIColor = .white
@objc func handleOpenTermsAndConditions() {
if currentBackgroundColor == .white {
self.currentBackgroundColor = .black
self.view.backgroundColor = .black
} else {
self.currentBackgroundColor = .white
self.view.backgroundColor = .white
}
}
使用枚举状态的方法
如果您想限制颜色,您可以创建具有可用颜色的 Enum
。我认为将它与一些像 .white
| 这样简单的逻辑一起使用有点过分了。 .black
.
第 1 步:创建一个 enum
。
enum AvailableColor {
case white
case black
func currentUIColor() -> UIColor {
switch self {
case .white:
return UIColor.white
case .black:
return UIColor.black
}
}
}
第 2 步:实例化状态变量并使用 enum
逻辑
var currentColorState : AvailableColor = .white
@objc func handleOpenTermsAndConditions() {
if currentColorState == .white {
currentColorState = .black
} else {
currentColorState = .white
}
self.view.backgroundColor = currentColorState.currentUIColor()
}
不要试图通过比较颜色来使用视图的内容来确定下一个状态。
将 model 与 view 分开,并保留一个具有当前状态的单独 属性。您可以使用一个简单的布尔标志 isBlack
,或者您可以创建一个枚举:
enum BackgroundState {
case black, white
}
如果您想要另一个 color/state,它有一个更容易扩展的小优势。 (CaseIterable
可能也会有帮助。)