2015-06-01 26 views
0

我正在面临制作计时器应用的难题,所以我认为现在我已经解决了这个问题,我可以帮助其他面临问题的人。所以基本上这个应用程序倒数到当前时间的特定日期。由于堆栈溢出允许Q和A格式,我希望可以帮助你。请参阅评论以获得解释。如何进行倒计时Swift

+1

Q和A是受欢迎的。但它应该是一个真正的问题,达到所有职位预期的相同标准,比较http://meta.stackoverflow.com/questions/284765/was-i-wrong-to-answer-my-own-question 。 “如何进行倒计时”太广泛了。 –

+0

谢谢。这就是我一直在寻找的。 –

回答

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"