0

I'm looking to add some final touches to the user experience of a game I'm writing in Swift. Currently I've got a gameOVerViewController that is activated when the game is over, on that screen the score and highscore are displayed with an interstitial ad after a few seconds.

I've also got a replay UIButton that unwinds the segue and restarts the game.

What I'm looking for is a timer on the replay button that make the button active after three seconds, similar to what you may see on an interstitial advert where you can see the seconds counting down before you can have any interaction with the object.

--------------- Updated with code

Hello again,

I've partially made the code work, the UIButton has had it's accesabilitty enabled box unchecked with the below code in my gameOverViewController file.

class GameOverViewController: UIViewController {

@IBOutlet weak var button: UIButton!

}

override func viewDidLoad() {
    super.viewDidLoad()

            self.button.enabled = false
    NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "enableButton", userInfo: nil, repeats: false)

//lots of code here to bring up google interstitial advert

}

func enableButton() {
    self.button.enabled = true
}

The button is correctly greyed out for three seconds then turns blue, however once clicked the viewController hangs then crashes. The error brings up AppDelegate with the SIGABRT error. This is the extract from the output field...

20  libdyld.dylib                       0x0000000105fa5145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

The button is part of a segue that once pressed changes the ViewController back to GameViewController, here is the code on GameViewController

    @IBAction func replay(segue: UIStoryboardSegue) {
}

Without the code that enables the button after three seconds the replay works fine. Any ideas?

Rich Townsend
  • 571
  • 1
  • 5
  • 21
  • 1
    would [the answer to this related question](http://stackoverflow.com/a/26756532/981049) be what you are looking for? – Michael Dautermann Aug 06 '15 at 06:21
  • What you have tried and what is not working? – Dharmesh Kheni Aug 06 '15 at 06:22
  • @DharmeshKheni I've used the code from the tutorial that Michael posted above, it seems to be almost working but now crashes, I've edited the post to include what I've tried and what is not working as requested. Thank you. – Rich Townsend Aug 06 '15 at 21:08

2 Answers2

0

You can disable the replay button initially and use performSelector:AfterDelay to enable it later.

toofani
  • 1,650
  • 13
  • 16
0

What you can do is use NSTimer to count down 3 seconds, and each second, replace the button with one that has the current seconds left. To make the button inactive during that time, you can use an alpha that is less than 1 (for visual purposes) and disable user interaction with it. Hope that helps! If you need help with writing the code, I can help.