如何使Swift倒计时

Jul*_* E. 2 timer swift

我正面临着制作计时器应用程序的困境,所以我想现在我已经解决了它,我可以帮助那些面对问题的人.所以基本上这个应用程序倒计时到当前时间的特定日期.由于堆栈溢出允许Q和A格式,我希望可以帮助您.请参阅注释以获得解释.

Mod*_*ort 11

使用在计时器上计算的倒计时和前导零字符串格式进行清理和更新。

    let futureDate: Date = {
        var future = DateComponents(
            year: 2020,
            month: 1,
            day: 1,
            hour: 0,
            minute: 0,
            second: 0
        )
        return Calendar.current.date(from: future)!
    }()

    var countdown: DateComponents {
        return Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: futureDate)
    }

    @objc func updateTime() {
        let countdown = self.countdown //only compute once per call
        let days = countdown.day!
        let hours = countdown.hour!
        let minutes = countdown.minute!
        let seconds = countdown.second!
        countdownLabel.text = String(format: "%02d:%02d:%02d:%02d", days, hours, minutes, seconds)
    }

    func runCountdown() {
        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
    }
Run Code Online (Sandbox Code Playgroud)


Jul*_* E. 10

这是我如何设法为特定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
Run Code Online (Sandbox Code Playgroud)

如果你面对与我一样的挣扎,希望能帮到你们


小智 7

清理/更新已接受答案的最新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"
Run Code Online (Sandbox Code Playgroud)