The other questions I've seen on this didn't help. I want to be able to start, pause, and then resume the NSTimer. I know how to start the NSTimer. I also know that you can't 'pause' the NSTimer but you can invalidate it. But what would I have to do if I wanted to resume it keeping the same time I had when I stopped the timer? Here's the code:
var startTimer = NSTimeInterval()
var timer = NSTimer()
@IBOutlet var displaylabel: UILabel!
@IBAction func start(sender: UIButton) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true)
startTimer = NSDate.timeIntervalSinceReferenceDate()
}
@IBAction func stop(sender: UIButton) {
timer.invalidate()
}
@IBAction func resume(sender: UIButton) {
// Code needed
}
func updateTime() {
let currentTime = NSDate.timeIntervalSinceReferenceDate()
var elapsedTime: NSTimeInterval = currentTime - startTimer
let minutes = UInt8(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
let fraction = UInt8(elapsedTime * 100)
let strMinutes = String(format: "%02d", minutes)
let strSeconds = String(format: "%02d", seconds)
let strFraction = String(format: "%02d", fraction)
displaylabel.text = "(strMinutes):(strSeconds).(strFraction)"
}
Thanks in advance. Anton
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…