2016-12-27 47 views
0

我有一個UIButton,當點擊時我想讓它閃爍一段背景色,然後在一段時間後返回到原來的顏色。如何閃光顏色?

它的舊顏色不總是相同的,所以我做了一個類變量。

這裏是我當前的代碼,但我不知道如何實現延時:

class ViewController: UIViewController { 
    ///stuff 
    var oldColor: UIColor? 

    @IBAction func buttonPressed(_ sender: UIButton) { 
     oldColor = sender.backgroundColor 
     flashColor(sender, UIColor.green) 
    } 

    func flashColor(btn: UIButton, color: UIColor) { 
     btn.backgroundColor = color 
     wait(100ms) //I really have no idea how to do this part 
     btn.backgroundColor = oldColor 
    } 

} 

回答

4

使用我delay功能:

btn.backgroundColor = color 
    delay(0.1) { 
     btn.backgroundColor = oldColor 
    } 
+0

你應該認真地把這個Github上要義!這太神奇了,謝謝,我會接受這一次,讓我 –