我曾尝试在 Swift 中倒计时,但它会在几秒钟内返回输入。我想以小时为单位返回输入。如何将秒转换为小时?
请在下面查看我的代码:
var remainingTime: NSTimeInterval = 0
var endDate: NSDate!
var timer = NSTimer()
var timerCounter:NSTimeInterval!
override func viewDidLoad() {
super.viewDidLoad()
remainingTime = 7200.0 //--> Here I Want to give input in hours like 2 hours or 3 hours not in seconds.
let (h,m,s) = secondsToHoursMinutesSeconds(7200)
print ("\(h) Hours")
endDate = NSDate().dateByAddingTimeInterval(remainingTime) // set your future end date by adding the time for your timer
timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "updateLabel", userInfo: nil, repeats: true) // create a timer to update your label
}
func updateLabel()
{
timerlbl.text = endDate.timeIntervalSinceNow.hmmss
}
func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}