从倒数切换为倒数

时间:2018-09-02 14:35:45

标签: swift countdown

stackoverflow是我最后的机会。我希望你能帮助我。我的应用程序中存在一个倒计时,设置为2小时30分钟。当它达到0时,它开始计数最多30分钟。 30分钟结束后,倒计时返回2:30。除了定时器从倒数部分切换到倒数部分时,其他所有部分都工作正常。问题出在这里:计时器为零时变为负数。

 @objc func startTimer() {
    print("Countdown : \(totalTime)")
    countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}

 @objc func updateTime() {
    if (totalTime > 10800) {
        totalTime = totalTime - 10800
    }


    totalTime -= 1
    var time = totalTime


    //When in session...
    if (time > 9000) {

        //Calculate CountUP
        time = 10800 - time

        //if session has started
        if(isSessionActive == false) {
            isSessionActive = true
            lastFiveMinutes = false
            self.setSessionStarted()
        }
    } else {
        //If session has ended...
        if(isSessionActive == true) {
            isSessionActive = false
            lastFiveMinutes = false
            self.setSessionEnded()
        }
        //If last 5 minutes have started
        if(time < 300 && lastFiveMinutes == false) {
            lastFiveMinutes = true
            self.setLastFiveMinutes()
        }
    }
    countdownLabel.text = "\(timeFormatted(time))"

    //update Participant Count every x second, ONLY if in last 5 Minutes or Session is active
    if (lastFiveMinutes == true || isSessionActive == true) {
        if (totalTime % 5 == 0) {
            setParticipants(n: httpController.getPar())
        }
    } else {
        setParticipants(n: "...")
    }

}

我知道,“ var time = totalTime”部分不是必需的。

在Android中,除了-= 1部分外,代码完全相同,但可以正常工作。

*更新*

totalTime的初始值:      @objc var totalTime = 0

这是获得真实价值的下一部分。

@objc func initCountdown() {
    var currentSec = 0.0
    // Get Current Time from Server
    serverTimeReturn { (getResDate) -> Void in
        let dFormatter = DateFormatter()
        dFormatter.dateStyle = DateFormatter.Style.long
        dFormatter.timeStyle = DateFormatter.Style.long
        dFormatter.timeZone = NSTimeZone(abbreviation: "GMT")! as TimeZone
        let dateGet = dFormatter.string(from: getResDate! as Date)
        currentSec = Double((getResDate?.timeIntervalSince1970)!)

        print("Formatted Hour : \(dateGet)")

        let todaySec = Int(currentSec) % 86400
        print("Today Sec Hour : \(todaySec)")
        let countDownSec = 10800 - todaySec % 10800

        // Check status to init
        if(countDownSec > 9000) {
            self.isSessionActive = true
            self.setSessionStarted()
        } else {
            self.setSessionEnded()
        }
        if(countDownSec < 300) {
            self.setLastFiveMinutes()
            self.lastFiveMinutes = true
        }
        self.totalTime = countDownSec
    }
    print("Formatted Hour : \(totalTime)")
    startTimer()
}

1 个答案:

答案 0 :(得分:0)

我看不到任何代码可以加计数,所以我不奇怪它不起作用。我将介绍一个布尔属性isCountingDown来跟踪如何处理计时器,然后使用上下两个单独的方法

var isCountingDown: boolean

@objc func initCountdown() {
    //existing code

   isCountingDown = true
   startTimer()
}

@objc func updateTime() {
    if isCountingDown {
        doCountDown()
    } else {
        doCountUp()
    }
}

然后在doCountDown()doCountUp()中检查时间是0还是30,并相应地更新isCountingDown

func doCountDown {
   if (totalTime > 10800) {
       totalTime = totalTime - 10800
   }

   totalTime -= 1
   if totalTime == 0 {
       isCountingDown = false
       //Maybe return here?
   }
   var time = totalTime
   //rest of code
}

func doCountUp() {
    totalTime += 1

    if totalTime == 30 {
        isCountingDown = true
       //Maybe return here?
   }
   //rest of code
}