0

I have a UIButton, and when tapped I want it to flash a background color, then return to its old color after some time.

Its old color isn't always the same, so I made a class variable.

Here is my current code, but I'm not sure how to achieve the time delay:

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
   }

}
jtbandes
  • 115,675
  • 35
  • 233
  • 266
Ronak Shah
  • 936
  • 9
  • 17

1 Answers1

4

Use my delay function:

   btn.backgroundColor = color
   delay(0.1) { 
       btn.backgroundColor = oldColor
   }
Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141