2017-08-29 225 views
0

我最近一直致力於製作類似於股票iOS設備上的秒錶應用程序。我有計時器工作,但我希望它遵循像Apple的秒錶一樣的外觀和確切的時間。截至目前,我只是顯示秒數。據我所知,必須有某種將數字轉換爲00:00.00格式。如何使用我的秒錶顯示確切的時間?

import UIKit 

class ViewController: UIViewController { 

    var counter = 0 
    var timer = Timer() 

    @IBOutlet weak var timerLbl: UILabel! 

    @IBAction func startBtn(sender: Any) { 
     timer.invalidate() 
     timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true) 

    } 

    @IBAction func stopBtn(sender: Any) { 
     timer.invalidate() 
     counter = 0 
    } 

    func updateTimer() { 
     counter += 1 
     timerLbl.text = "(counter)" 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
+0

可能重複[什麼格式的字符串,我在毫秒的iPhone日期字符串?](https://stackoverflow.com/questions/6456626/what-format-字符串 - 我使用毫秒 - 在日期字符串在iphone) – Wukerplank

+0

@Wukerplank是Objective-C,我正在尋找一個答案在Swift中。 – Dexter

+0

你一定在開玩笑吧。查找「NSDateFormatter」的文檔,並使用提供的答案中的格式字符串。 – Wukerplank

回答

1

在創建計時器您應該創建一個基準日期,然後每次定時器觸發,通過創建日期獲取當前時間,然後開始使用日曆的兩個日期之間的時間。你可以得到更多的信息,如何做到這一點here