How can I pause and resume NSTimer.scheduledTimerWithTimeInterval in swift?

前端 未结 9 1673
甜味超标
甜味超标 2020-12-10 01:06

I\'m developing a game and I want to create a pause menu. Here is my code:

self.view?.paused = true

but NSTimer.scheduledTimerWithT

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 01:32

    To Start:

    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateView"), userInfo: nil, repeats: true)
    

    To Resume:

    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateView"), userInfo: nil, repeats: true)
    

    To Pause:

    timer.invalidate
    

    This worked for me. The trick is that do not look for something like "timer.resume" or "timer.validate". Just use "the same code for starting a timer" to resume it after the pause.

提交回复
热议问题