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
You should use a CADisplayLink
for two reasons:
You'll update the screen as often as possible and not waste extra battery life trying to update it more often than possible. All iOS devices (except the iPad Pro under rare circumstances) update the screen at most 60 times per second. Scheduling your timer for 0.01 seconds means you're trying to update the screen at least 40 times per second more often than necessary.
You can pause and resume a CADisplayLink
.
Handling pause requires a little thought. What you want to do is record the time when you pause (separately from the start time), and then when you resume, increase the start time by the time elapsed since pausing.
class ViewController: UIViewController {
private var startTime = NSTimeInterval(0)
private var pauseTime = NSTimeInterval(0)
private var displayLink: CADisplayLink!
@IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
displayLink = CADisplayLink(target: self, selector: "displayLinkDidFire")
displayLink.paused = true
displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
}
@IBAction func startWasTapped(sender: AnyObject) {
startTime = NSDate.timeIntervalSinceReferenceDate()
displayLink.paused = false
}
@IBAction func resumeWasTapped(sender: AnyObject) {
startTime += NSDate.timeIntervalSinceReferenceDate() - pauseTime
displayLink.paused = false
}
@IBAction func stopWasTapped(sender: AnyObject) {
pauseTime = NSDate.timeIntervalSinceReferenceDate()
displayLink.paused = true
}
func displayLinkDidFire() {
let elapsedTime = NSDate.timeIntervalSinceReferenceDate() - startTime
label.text = String(format: "%02.0f:%05.2f", floor(elapsedTime / 60), elapsedTime)
}
}