swiftcocoa-touch

Make UIButton disappear after clicking it?


I want after I click on a button it should disappear, but I want it that it also will be back in a few seconds.

After I click on the button, the time should be random (something like 2 or 5 seconds) before the button will come back.

The buttons action is @IBAction func increaseCount(sender: AnyObject).


Solution

  • The code below will hide the button for a random time between 2-5 seconds

    @IBAction func increaseCount(button: UIButton) -> Void {
      button.isHidden = true
    
      let time = dispatch_time(DISPATCH_TIME_NOW, Int64(Double((arc4random_uniform(3) + 2)) * Double(NSEC_PER_SEC)))
      dispatch_after(time, dispatch_get_main_queue()) {
        button.isHidden = false
      }
    }
    

    .hidden was changed to .isHidden in Swift 3