Swift 从倒计时切换到倒计时
这是我最后的机会了。我希望你能帮助我。我的应用程序中有一个倒计时,设置为2小时30分钟。当它达到0时,它开始数到30分钟。30分钟结束后,倒数计时在2:30重新开始。除了计时器从倒计时部分切换到倒计时部分外,其他一切正常。问题是:计时器在达到零位时将变为负数Swift 从倒计时切换到倒计时,swift,countdown,Swift,Countdown,这是我最后的机会了。我希望你能帮助我。我的应用程序中有一个倒计时,设置为2小时30分钟。当它达到0时,它开始数到30分钟。30分钟结束后,倒数计时在2:30重新开始。除了计时器从倒计时部分切换到倒计时部分外,其他一切正常。问题是:计时器在达到零位时将变为负数 @objc func startTimer() { print("Countdown : \(totalTime)") countdownTimer = Timer.scheduledTimer(timeInterval:
@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: "...")
}
}
@objc func startTimer(){
打印(“倒计时:\(总时间)”)
countdownTimer=Timer.scheduledTimer(时间间隔:1,目标:self,选择器:#选择器(更新时间),用户信息:nil,重复:true)
}
@objc func updateTime(){
如果(总时间>10800){
totalTime=totalTime-10800
}
总时间-=1
var时间=总时间
//在会话中。。。
如果(时间>9000){
//计算倒计时
时间=10800-时间
//如果会话已启动
如果(isSessionActive==false){
isSessionActive=true
lastFiveMinutes=false
self.setSessionStarted()
}
}否则{
//如果会话已结束。。。
如果(isSessionActive==true){
isSessionActive=false
lastFiveMinutes=false
self.setSessionEnded()
}
//如果最后5分钟已经开始
如果(时间<300&&lastFiveMinutes==false){
lastFiveMinutes=真
self.setLastFiveMinutes()
}
}
countdownLabel.text=“\(timeFormatted(time))”
//仅在最近5分钟内或会话处于活动状态时,每隔x秒更新参与者计数
如果(lastFiveMinutes==true | | isSessionActive==true){
如果(总时间%5==0){
setParticipants(n:httpController.getPar())
}
}否则{
集合参与者(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()
}
@objc func initCountdown(){
var currentSec=0.0
//从服务器获取当前时间
serverTimeReturn{(getResDate)->中的Void
设dFormatter=DateFormatter()
dFormatter.dateStyle=DateFormatter.Style.long
dFormatter.timeStyle=DateFormatter.Style.long
dFormatter.timeZone=NSTimeZone(缩写:“GMT”)!作为时区
让dateGet=dFormatter.string(from:getResDate!as Date)
currentSec=Double((getResDate?.TimeIntervalnces1970)!)
打印(“格式化小时:\(日期获取)”)
让今天秒=整数(当前秒)%86400
打印(“今天秒小时:\(今天秒)”)
让countDownSec=10800-今天秒%10800
//检查初始化状态
如果(倒计时秒>9000){
self.isSessionActive=true
self.setSessionStarted()
}否则{
self.setSessionEnded()
}
如果(倒计时秒<300){
self.setLastFiveMinutes()
self.lastFiveMinutes=真
}
self.totalTime=countDownSec
}
打印(“格式化小时:\(总时间)”)
startTimer()
}
我没有看到任何向上计数的代码,因此我并不奇怪它不起作用。我将引入一个布尔属性isCountingDown
,以跟踪如何处理计时器,然后有两个单独的方法用于up和down
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
}
totalTime
的初始值是多少?@Carpsen90查看更新的答案。谢谢!我要试试这个。自由职业者说,我认为计数部分是通过补偿完成的。这就是部分:“计算CountUP/time=10800-time”实际上,当我在30分钟内打开应用程序时,CountUP部分工作得非常好。只有当我看到倒计时倒计时到0,然后变为0和负数时,错误才会出现。但是当我关闭应用程序并重新启动时,计数部分工作得非常好。