我認爲我很好與if語句,但顯然我仍然失去了一些東西。在線路timer.invalidate()
編譯器會抱怨「如果」聲明,條件將永遠不會被執行
永遠都不能像據我可以告訴我的語法是否正確執行
。
@IBAction func button(_ sender: Any) {
let timer = Timer.scheduledTimer(timeInterval:0.2 , target: self, selector: #selector(ViewController.imageSwitch) , userInfo: nil, repeats: true)
var buttonState = false
if buttonState == true {
timer.invalidate()
buttonState = false
}
if buttonState == false {
timer.fire()
buttonState = true
}
}
你到底想幹什麼?你將'buttonState'設置爲false,然後檢查它是否爲true。當然這不會是真的。 – Sweeper
刪除這行'var buttonState = false'並將其添加到方法'@IBAction func按鈕(_ sender:Any)'之外。 – Amit
您需要將var buttonState = false全局設置爲視圖控制器,否則此條件從不爲真。 – Hitesh