I have seen multiple questions similar to this; however, none of them resolved my problem. My timer is simply going too slowly. I am only using an interval of 0.01
seconds. Here is my code:
@IBOutlet var timerLabel: UILabel!
var miliseconds = 0
var seconds = 0
func updateLabel() {
if miliseconds == 0 {
timerLabel.text = "\(seconds).00"
} else if miliseconds < 10 {
timerLabel.text = "\(seconds).0\(miliseconds)"
} else {
timerLabel.text = "\(seconds).\(miliseconds)"
}
}
var timer = NSTimer()
func updateTime() {
miliseconds++
if miliseconds == 100 {
miliseconds = 0
seconds++
}
updateLabel()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if timerState == 1 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
timerLabel.textColor = UIColor.blackColor()
timerState = 2
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if timerState == 0 {
miliseconds = 0
seconds = 0
updateLabel()
timerLabel.textColor = UIColor.greenColor()
timerState = 1
} else if timerState == 2 {
timerState = 0
timer.invalidate()
}
}
var timerState = 0
//timerState of 0 = Has not started
//timerState of 1 = About to start
//timerState of 2 = Timing
I have also tried using delays:
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
I called updateTime
in viewDidLoad
, and at the end of updateTime
, I added:
delay(0.01) { () -> () in
self.updateTime()
}
However, it still went at the same speed as before.
How can I fix this issue? If I missed a question while researching, please let me know. Thanks!
From my comment above ... (with encouragement)
NSTimer - from apple developer docs:
There is a whole mess of problems here. Let's clean this up.
First of all, don't use a timer to update the label. Use a
CADisplayLink
. A display link synchronizes with the screen refresh interval, which is 1/60 of a second on most iOS devices (not 1/100), so you don't do extra work:Next, don't try to track the elapsed time by incrementing a counter when the timer (or link) fires, because the timer or link is not guaranteed to fire as often as you requested. Instead, store the time at which the timer was started, and the time at which it was stopped (if it was stopped):
Track the state using an
enum
instead of mysterious hard-coded numbers. And since the label color depends only the state, add a property to the state that gives the label color for that state:The elapsed time depends on the state, so add a method to compute it:
Use a format string to convert the elapsed time to a string when updating the label:
Changing the timer state can change both the label color and the elapsed time, so update the label color and the label text when the state changes:
When a touch begins, create the display link if needed, then update the state. The state's
didSet
will handle updating the label as necessary:When a touch ends, start the timer if necessary:
Here's how you create the display link:
And here's the method the display link will call: