swift - 从倒计时切换到正计时

标签 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 总时间 = 0

这是它获得真正值(value)的下一部分。

@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()
}

最佳答案

我没有看到任何向上计数的代码,所以我对它不起作用并不感到惊讶。我将引入一个 bool 属性 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
}

关于swift - 从倒计时切换到正计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52137993/

相关文章:

xcode - 使用 Swift 的 TableViewController 和 CustomCell

swift - 泛型继承和 Swift 编译器

Javascript 开放时间倒计时

android CountDownTimer - 最后一个 onTick 未调用 - 使用什么干净的解决方案?

ios - Swift:计时器在前台/后台运行

swift - 如何从数组的数组中获取值

ios - 使用 CLLocationManager 获取用户位置时出错

javascript - 清除倒计时器

javascript - 修改倒计时脚本以允许每页多个倒计时

swift - iOS App AppDelegate 中的动画