我正在面臨製作計時器應用的難題,所以我認爲現在我已經解決了這個問題,我可以幫助其他面臨問題的人。所以基本上這個應用程序倒數到當前時間的特定日期。由於堆棧溢出允許Q和A格式,我希望可以幫助你。請參閱評論以獲得解釋。如何進行倒計時Swift
0
A
回答
8
這是我如何設法創建一個倒數計時器到特定NSDate的解決方案,因爲SO允許Q和A樣式答案。
// here we set the current date
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: date)
let hour = components.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day
let currentDate = calendar.dateFromComponents(components)
// here we set the due date. When the timer is supposed to finish
let userCalendar = NSCalendar.currentCalendar()
let competitionDate = NSDateComponents()
competitionDate.year = 2015
competitionDate.month = 6
competitionDate.day = 21
competitionDate.hour = 08
competitionDate.minute = 00
let competitionDay = userCalendar.dateFromComponents(competitionDate)!
// Here we compare the two dates
competitionDay.timeIntervalSinceDate(currentDate!)
let dayCalendarUnit: NSCalendarUnit = (.CalendarUnitDay | .CalendarUnitHour | .CalendarUnitMinute)
//here we change the seconds to hours,minutes and days
let CompetitionDayDifference = userCalendar.components(
dayCalendarUnit, fromDate: currentDate!, toDate: competitionDay,
options: nil)
//finally, here we set the variable to our remaining time
var daysLeft = CompetitionDayDifference.day
var hoursLeft = CompetitionDayDifference.hour
var minutesLeft = CompetitionDayDifference.minute
希望幫助你們,如果你面臨着同樣的鬥爭,因爲我有
3
這爲我工作。 唯一令我困擾的是它並沒有真正倒計時,因爲用戶必須刷新頁面才能重新計算。當用戶在UITableView上滾動單元格時,您可以看到它「計數」,因爲單元格會刷新視圖。 另一件事是,我有NSTimeZone的當前日期「GMT + 2:00」,因爲它適用於我的時間,但只是因爲我還沒有想出如何使用設備NSTimeZone呢。
let releaseDate = "2015-05-02'T'22:00:00:000Z"
let futureDateFormatter = NSDateFormatter()
futureDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date: NSDate = futureDateFormatter.dateFromString(releaseDate!)!
let currentDate = NSDate();
let currentFormatter = NSDateFormatter();
currentFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
currentFormatter.timeZone = NSTimeZone(abbreviation: "GMT+2:00")
let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: currentDate, toDate: date, options: NSCalendarOptions.init(rawValue: 0))
let countdown = "\(diffDateComponents.month) m: \(diffDateComponents.day) d: \(diffDateComponents.hour) h: \(diffDateComponents.minute) min"
3
已清理/更新了接受答案的最新Swift版本。
// here we set the current date
let date = NSDate()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .month, .year, .day], from: date as Date)
let currentDate = calendar.date(from: components)
let userCalendar = Calendar.current
// here we set the due date. When the timer is supposed to finish
let competitionDate = NSDateComponents()
competitionDate.year = 2017
competitionDate.month = 4
competitionDate.day = 16
competitionDate.hour = 00
competitionDate.minute = 00
let competitionDay = userCalendar.date(from: competitionDate as DateComponents)!
//here we change the seconds to hours,minutes and days
let CompetitionDayDifference = calendar.dateComponents([.day, .hour, .minute], from: currentDate!, to: competitionDay)
//finally, here we set the variable to our remaining time
let daysLeft = CompetitionDayDifference.day
let hoursLeft = CompetitionDayDifference.hour
let minutesLeft = CompetitionDayDifference.minute
print("day:", daysLeft ?? "N/A", "hour:", hoursLeft ?? "N/A", "minute:", minutesLeft ?? "N/A")
//Set countdown label text
countDownLabel.text = "\(daysLeft ?? 0) Days, \(hoursLeft ?? 0) Hours, \(minutesLeft ?? 0) Minutes"
相關問題
- 1. 如何進行倒計時?
- 2. 如何使用NSTimer進行倒計時?
- 3. 如何進行全球倒計時?
- 4. 在swift和spritekit倒計時
- 5. 倒計時,進度
- 6. 進入倒計時
- 7. jQuery倒計時進度條
- 8. 如何使用NSDate運行倒計時?
- 9. 如何在每月的每個星期日進行倒計時?
- 10. 如何使用秒和毫秒進行倒計時
- 11. 如何在C#中用Console.Clear()和多線程進行倒計時
- 12. 如何在手錶併發症中進行倒計時?
- 13. 如何在本月的每個星期五進行倒計時?
- 14. 如何在對話框中進行倒計時,並在對話框再次打開時恢復倒計時
- 15. swift:停止倒數計時器爲零
- 16. 如何在java(秒錶Java)中進行倒計時CountDown計時器
- 17. 使用UIDatePicker在iOS中進行計時器倒計時
- 18. 如何倒數計時器
- 19. 倒計時如何重複?
- 20. 如何倒計時`MediaStore.EXTRA_DURATION_LIMIT`?
- 21. 如何停止倒計時
- 22. Android如何使倒計時像倒計時鐘?
- 23. 如何使用倒數計時器停止遊戲 - iOS [SWIFT] -
- 24. jQuery倒計時 - 每日倒計時,還有次要倒計時?
- 25. 如何在列表完成計數後操作列表進行倒計時?
- 26. 時間戳倒計時進入負面
- 27. 執行倒計時js倒計時在php代碼
- 28. JQuery倒計時器不倒計時
- 29. Js倒計時只倒計時一次
- 30. C#timer_Tick()倒計時2步倒計時
Q和A是受歡迎的。但它應該是一個真正的問題,達到所有職位預期的相同標準,比較http://meta.stackoverflow.com/questions/284765/was-i-wrong-to-answer-my-own-question 。 「如何進行倒計時」太廣泛了。 –
謝謝。這就是我一直在尋找的。 –