Format realtime stopwatch timer to the hundredth using Swift

前提是你 提交于 2019-11-28 13:46:22

For rapid UI updates you should use a CADisplayLink. Anything faster than the display refresh rate is a waste of processing power since it physically cannot be displayed. It also provides a timestamp of the previous frame so you can try to predict when the next frame will be.

You're calculating CACurrentMediaTime() - timerStarted + elapsedTime multiple times. I would recommend doing it only once and saving it in a local variable.

Consider using NSDateComponentsFormatter. Try to reuse one instance of the formatter rather than creating a new one each time (which is usually the most expensive part). Overall, the less string manipulation you can do, the better.

You can check CACurrentMediaTime at the beginning and end of your display method to see how long it takes. Ideally it should be much less than 16.6ms. Keep an eye on the CPU usage (and general power consumption) in the Xcode debug navigator.

I was solving the same problem today and found this answer. The Rob's and jtbandes' advices are helped a lot and i was able to assemble the clean and working solution from around the internet. Thanks you guys. And thanks to mothy for the question.

I've decided to use CADisplayLink because there is no point in firing timer's callback more often than the screen updates:

class Stopwatch: NSObject {
    private var displayLink: CADisplayLink!
    //...

    override init() {
        super.init()

        self.displayLink = CADisplayLink(target: self, selector: "tick:")
        displayLink.paused = true
        displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
        //...
    }
    //...
}

I'm tracking time by incrementing the elapsedTime variable by displayLink.duration each tick:

var elapsedTime: CFTimeInterval!

override init() {
    //...
    self.elapsedTime = 0.0
    //...
}

func tick(sender: CADisplayLink) {
    elapsedTime = elapsedTime + displayLink.duration
    //...
}

Time-formatting is done through NSDateFormatter:

private let formatter = NSDateFormatter()

override init() {
//...
        formatter.dateFormat = "mm:ss,SS"
}

func elapsedTimeAsString() -> String {
        return formatter.stringFromDate(NSDate(timeIntervalSinceReferenceDate: elapsedTime))
}

The UI can be updated in the callback closure which Stopwatch calls on every tick:

var callback: (() -> Void)?

func tick(sender: CADisplayLink) {
    elapsedTime = elapsedTime + displayLink.duration

    // Calling the callback function if available
    callback?()
}

And that's all you need to do in the ViewController to utilize the Stopwatch:

let stopwatch = Stopwatch()
stopwatch.callback = self.tick

func tick() {
   elapsedTimeLabel.text = stopwatch.elapsedTimeAsString()
}

Here is the gist with the full code of Stopwatch and usage guide: https://gist.github.com/Flar49/06b8c9894458a3ff1b14

I hope that this explanation and gist will help others who will stumble upon this thread in the future with the same problem :)

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