Swift NSTimer dynamically changing interval [duplicate]

无人久伴 提交于 2020-03-04 08:54:11

问题


How can I change the time interval on an NSTimer?

var difficulty: Double = 1.0

override func viewDidLoad() {
    super.viewDidLoad()

    _ = NSTimer.scheduledTimerWithTimeInterval(difficulty, target: self, selector: #selector(hideAllBtns), userInfo: nil, repeats: true)

}

I run a switch statement which gets called each time from hideallbuttons and that then updates the difficulty.

func getDifficulty (score : Double) -> Double {

    switch score {

    case 0...100: return(1.0)
    case 100...200: return (0.8)
    case 200...300: return (0.5)
    case 300...400: return (0.2)
    default: return (1.0)

    }
}

However NSTimer continues to fire at 1.0s I presume it creates a copy upon first run and sticks to that value, I have tried to create a variable in the timer call but that doesn't work.

Is there a simple way to adjust the rate dynamically at which the timer fires?

This is just a practice project as I am still learning, so I understand there is probably a much better way to structure what I am trying to do. I think a dynamically adjusting timer would be useful for future use.


回答1:


You can't, once you fire the timer, it runs.

I am not sure what exactly you need but you have two options:

  1. Invalidate the timer, and fire a new one with new time interval, you can even declare the timer in the class, and just override the value (after you invalidate).

  2. Set the repeat value to false and fire new timer every time.

Take a look at this example:

class ViewController: UIViewController {
    var difficulty: Double = 1.0
    var timer = NSTimer()

    override func viewDidLoad() {
        activateTimer()
    }

    func activateTimer(){
       timer = NSTimer.scheduledTimerWithTimeInterval(difficulty, target: self, selector: Selector(self.timerMethod()), userInfo: nil, repeats: true)
    }

    func timerMethod() {
        print("Timer method called")
    }

    func endTimer() {
        timer.invalidate()
    }

    func increaseDifficulty() {
      difficulty = 2.0
      endTimer()
      activateTimer()
    }

}



回答2:


You can't change the interval on an NSTimer object once it's created.

To get around this, you could increase the rate at which your timer fires and manually check if the desired amount of time has passed. This allows you to define a dynamically updating amount of time- mimicking a dynamic timer:

var lastTriggered: NSDate!
var dynamicInterval = 1.0
var timerInterval = 0.1

override func viewDidLoad() {
    super.viewDidLoad()

    lastTriggered = NSDate()
    _ = NSTimer.scheduledTimerWithTimeInterval(timerInterval, target: self, selector: #selector(hideAllBtns), userInfo: nil, repeats: true)
}

func hideAllBtns() {
    let currentDate = NSDate()
    if(currentDate.timeIntervalSinceDate(lastTriggered) >= dynamicInterval) {
        // This will only be called every dynamicInterval seconds
        // Now call getDifficulty and/or update your dynamicInterval
        lastTriggered = currentDate
    }
}


来源:https://stackoverflow.com/questions/38728011/swift-nstimer-dynamically-changing-interval

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!