我试图从后台视图向ViewController.swift添加一个@IBAction,以响应Touch Down事件。我的IBAction将使序列为黑色->白色->浅灰色->深灰色->红色->黑色。当用户点击屏幕时,此序列应无限期重复。为了我自己,我不知道为什么它不起作用。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
self.changeBackground(self)
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func changeBackground(_ sender: Any) {
let button : UIButton = sender as! UIButton
button.isSelected = !button.isSelected
if (button.isSelected && self.view.backgroundColor == UIColor.black){
self.view.backgroundColor = UIColor.white
}
if (button.isSelected && self.view.backgroundColor == UIColor.white){
self.view.backgroundColor = UIColor.lightGray
}
if (button.isSelected && self.view.backgroundColor == UIColor.lightGray){
self.view.backgroundColor = UIColor.darkGray
}
if (button.isSelected && self.view.backgroundColor == UIColor.darkGray){
self.view.backgroundColor = UIColor.red
}
if (button.isSelected && self.view.backgroundColor == UIColor.red){
self.view.backgroundColor = UIColor.black
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}