从倒计时切换到计数

问题描述 投票:0回答:1

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部分,但它正在工作。

*更新*

总时间的初始值:@objc war total Time = 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()
}
swift countdown
1个回答
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
}
© www.soinside.com 2019 - 2024. All rights reserved.