【发布时间】:2021-05-27 08:59:42
【问题描述】:
我是快速编程的新手,在解决这个项目时遇到了麻烦。我需要让按钮背景每次单击时都会改变颜色,如果它只有两种颜色但不超过这种颜色,我已经设法解决它。任何帮助表示赞赏。这就是我设法使用两种颜色的方法。
import UIKit
class ViewController: UIViewController {
var lightOn = true
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
@IBAction func buttonPressed(_ sender: Any) {
lightOn.toggle()
updateUI()
}
func updateUI() {
if lightOn {
view.backgroundColor = .orange
} else {
view.backgroundColor = .yellow
}
}
}
【问题讨论】:
-
创建一个
UIColor数组并使用一个Int索引,该索引会在点击按钮时增加? -
你想要随机颜色吗?
-
是的,aheze,任意三种随机颜色。
标签: swift button background-color