我想用这段代码合并一些东西。我的目标是在标签中添加一个价格值,该价格在倒计时开始时一直在不断下降。例如:CurrentPrice - 第一秒的DiscountPerSecond,然后是第二等的CurrentPrice - DiscountPerSecond * 2 ......直到倒计时结束。
var timerCounter:NSTimeInterval!
func updateTime(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = interval / 3600
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
func startTimer(hour:Int) {
timerCounter = NSTimeInterval(hour * 60 * 60)
let aSelector : Selector = "onTimer:"
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
}
func onTimer(timer:NSTimer!) {
countdownShow.text = updateTime(timerCounter)
timerCounter!--
if (timerCounter == 0) {
timer.invalidate()
countdownShow.text = "Event Closed!"
}
}
答案 0 :(得分:1)
一些注意事项:
price = listPrice - (maximumDiscount * proportionOfTimeElapsed)
)NSDateComponentsFormatter
格式化剩余时间。