我是ios和swift的新手但不幸的是我无法弄清楚如何计算倒数计时器。我做了什么? .it是完全的,如果我从当前机器时间设置。但不会打扮时间。我知道它应该不起作用。但我不知道我怎么能让它工作。谢谢
// MARK:- Timer
let startFormatter = NSDateFormatter()
let endFormatter = NSDateFormatter()
let userClander = NSCalendar.currentCalendar()
let requestComponent: NSCalendarUnit = [
NSCalendarUnit.Month,
NSCalendarUnit.Day,
NSCalendarUnit.Hour,
NSCalendarUnit.Minute,
NSCalendarUnit.Second
]
func printTime(){
//let startTime = NSDate()
startFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
endFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let requestTimeString = "2017-01-16 12:55:27"
let requestTime = startFormatter.dateFromString(requestTimeString)
let endTimeString = "2017-01-16 12:58:27"
let endTime = endFormatter.dateFromString(endTimeString)
print(endTime)
let timeDifferance = userClander.components(requestComponent, fromDate: requestTime!, toDate: endTime!, options: [])
print(timeDifferance.minute)
displayCountDownTimerLabel.text = "\(timeDifferance.minute):\(timeDifferance.second)"
}
func start() {
let timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(ViewController.printTime), userInfo: nil, repeats: true)
timer.fire()
}
答案 0 :(得分:0)
let aSelector : Selector = #selector(updateTime)
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: aSelector, userInfo: labelCalIndex, repeats: true)
func updateTime()
{
let currentTime = NSDate.timeIntervalSinceReferenceDate()
/* ----------------------------------------------------------------
Find the difference between current time and start time.
---------------------------------------------------------------- */
var elapsedTime: NSTimeInterval = currentTime - startTime
/* ------------------------------------------------
calculate the hours in elapsed time.
------------------------------------------------ */
let hour = Int32( elapsedTime / 3600.0 )
elapsedTime -= (NSTimeInterval(hour) * 3600)
/* ------------------------------------------------
calculate the minutes in elapsed time.
------------------------------------------------ */
let minutes = Int32(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
/* ------------------------------------------------
calculate the seconds in elapsed time.
------------------------------------------------ */
let seconds = Int32(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
/* ------------------------------------------------------------------------------------------------------
Add the leading zero for minutes, seconds and millseconds and store them as string constants
------------------------------------------------------------------------------------------------------ */
let strMinutes = String(format: "%02d", minutes)
let strSeconds = String(format: "%02d", seconds)
let strHour = String(format: "%02d", hour)
/* -------------------------------------------------------------------------------------------
Concatenate minuets, seconds and milliseconds as assign it to the UILabel
------------------------------------------------------------------------------------------- */
let timerSting = "\(strHour):\(strMinutes):\(strSeconds)"
if minutes == 3
{
InvalidateTimer()
}
}
将您的开始时间保存在NSInterval中。
答案 1 :(得分:0)
如果您想要在指定的持续时间内使用3分钟计时器或计时器,则无需使用NSDate
。
Swift 3
如果你想要一个3分钟的计时器:
@IBOutlet weak var timerLabel: UILabel!
var currentTime : Int!
var timer : Timer!
override func viewDidLoad() {
super.viewDidLoad()
//total time in seconds.
currentTime = 180
timerLabel.text = convertSecondsToMinutes(currentTime)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateLabel), userInfo: nil, repeats: true)
}
func updateLabel(){
currentTime = currentTime - 1
timerLabel.text = convertSecondsToMinutes(currentTime)
//0 seconds left so kill the timer
if(currentTime == 0){
timer.invalidate()
}
}
func convertSecondsToMinutes(_ seconds:Int) -> String{
//one minute has 60 seconds soo :D
let minutes = seconds/60
let secondsvalue = Double(seconds).truncatingRemainder(dividingBy: 60)
//return string in appropriate format
return "\(minutes):\(Int(secondsvalue))"
}
答案 2 :(得分:0)
// MARK: - 定时器
let startFormatter = NSDateFormatter()
let endFormatter = NSDateFormatter()
let userClander = NSCalendar.currentCalendar()
let requestComponent: NSCalendarUnit = [
NSCalendarUnit.Month,
NSCalendarUnit.Day,
NSCalendarUnit.Hour,
NSCalendarUnit.Minute,
NSCalendarUnit.Second
]
var count = 0
func printTime(){
if(count > 0){
let minutes = String(count / 60)
let seconds = String(count % 60)
print(minutes)
print(seconds)
displayCountDownTimerLabel.text = minutes + ":" + seconds
count -= 1
}
else{
displayCountDownTimerLabel.text = "00:00"
}
}
func start() {
startFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
endFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let requestTimeString = "2017-01-16 12:58:27"
let requestTime = startFormatter.dateFromString(requestTimeString)
let endTimeString = "2017-01-16 12:58:56"
let endTime = endFormatter.dateFromString(endTimeString)
let timeDifferance = userClander.components(requestComponent, fromDate: requestTime!, toDate: endTime!, options: [])
print("\(timeDifferance.hour):\(timeDifferance.minute):\(timeDifferance.second)")
if timeDifferance.hour == 0 && timeDifferance.minute <= 3 {
count = (timeDifferance.minute * 60 ) + timeDifferance.second
let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.printTime), userInfo: nil, repeats: true)
timer.fire()
}else{
print("request time out ")
}
}