我曾尝试在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)
}