Format realtime stopwatch timer to the hundredth using Swift

前端 未结 2 971
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 18:32

I have an app using an NSTimer at centisecond (0.01 second) update intervals to display a running stopwatch in String Format as 00:00.00 (mm:ss.SS). (Basically cloning the i

相关标签:
2条回答
  • 2020-12-11 19:07

    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 :)

    0 讨论(0)
  • 2020-12-11 19:12

    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.

    0 讨论(0)
提交回复
热议问题