本文介绍了如何倒计时到 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在制作计时器应用程序时遇到了困难,所以我认为现在我解决了这个问题,我可以帮助其他面临问题的人.所以基本上这个应用程序从当前时间倒计时到特定日期.由于堆栈溢出允许 Q 和 A 格式,我希望可以帮助您.请参阅注释以获取解释.

I was facing the struggle of making a timer app, so I thought that now that I solved it I could help others who face the problem. So basically this app counts down to a specific date from the current time. As stack overflow allows a Q and A format I hope that can help you. See the comments for explanations.

推荐答案

这是我如何设法为特定 NSDate 创建倒数计时器的解决方案,因为 SO 允许 Q 和 A 样式答案.

Here is the solution of how I managed to create a countdown timer to a specific NSDate, for SO allows Q and A Style Answers.

// 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

如果你和我一样面临同样的困境,希望能帮到你们

Hope that helps you guys if you're facing the same struggle as I have

这篇关于如何倒计时到 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:46