首页 > 解决方案 > 计时器倒计时 6 小时并在时间到时停止,然后在按下按钮时再次开始倒计时 6 小时

问题描述

我需要一个计时器来倒计时 6 个小时。

.它必须在时间到时重置

.它必须在按下按钮时激活。

.它必须在应用程序打开或关闭时继续运行。

我有一个日期倒计时计时器,还有另一个倒计时计时器,它仅在应用程序处于活动状态时才有效。

如何制作一个计时 6 小时的计时器?

计时器倒计时到日期⬇︎

var timeCount: NSDate?
var timer = Timer()

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)

@objc func countdown()
{
let endTime = "08-10-2020 18:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
timeCount = (formatter.date(from: endTime))! as NSDate

let currentDate = Date()
let calendar = Calendar.current

let diffDateComponents = calendar.dateComponents([.hour, .minute, .second], from: currentDate, to: timeCount! as Date)

let countdown = "\(diffDateComponents.hour ?? 0):\(diffDateComponents.minute ?? 0):\(diffDateComponents.second ?? 0)"

let hourS = diffDateComponents.hour!
let minuteS = diffDateComponents.minute!
let secondS = diffDateComponents.second!

print(countdown)

if countdown == "0:0:0"
{
   button.setTitle("PRESS", for: .normal)
   button.backgroundColor = .green
   timer.invalidate()
}
else
{
   button.setTitle("PRESS WHEN TIME RUNS OUT" + "\n\(String(format: "%02d:%02d:%02d", hourS, minuteS, secondS))", for: .normal)
   button.backgroundColor = .red
}
}

计时器倒计时仅在应用程序处于活动状态时⬇︎

var timer = Timer()
var hours = 06
var minutes = 00
var secs = 00


timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)

@objc func countdown()
{
    secs = secs-1
    if (secs<0)
    {
        secs = 59
        minutes=minutes-1
        if (minutes<0)
        {
            minutes = 59
            hours = hours-1
            if (hours<0)
            {
                hours = 0
                minutes = 0
                secs = 0
                
                timer.invalidate()
            }
        }
    }

var hourp: String
var minutesp: String
var secp: String
    
if(hours<10)
{
hourp = "0" + String(describing: hours)
}
else
{
hourp = String(describing: hours)
}
if(minutes<10)
{
minutesp = "0" + String(describing: minutes)
}
else
{
minutesp = String(describing: minutes)
}
if(secs<10)
{
secp = "0" + String(describing: secs)
}
else
{
secp = String(describing: secs)
}
let timing = hourp + ":" + minutesp + ":" + secp
    
print(timing)

if hours == 0 && minutes == 0 && secs == 0
{
button.setTitle("PRESS", for: .normal)
button.backgroundColor = .green
else
{
button.setTitle("PRESS WHEN TIME RUNS OUT" + "\n\(timing)" , for: .normal)
button.backgroundColor = .red
}

}

标签: swifttimercountdownnsdatecomponents

解决方案


You can get the time and save it when pressed the button then you can take the time in viewdiload and compare them

It's like this;

First create 2 variable one for current time and other one is for compare

var currentTime = Date()
var compareTime = Date().addingTimeInterval(21600) //6 hours is 21600 second

Now we need to save the time when clicked the button;

UserDefaults.standard.set(currentTime, forKey: "time")

Now We need to compare these times;

override func viewDidLoad() {
    super.viewDidLoad()
    var time = UserDefaults.standard.object(forKey: "time")
        if let newTime = time as? Date {
        if let difference = Calendar.current.dateComponents([.hour], from: newTime, to: compareTime).hour {
            if difference > 6 {
              //make whatever you want if 6 hours is finish
            }
               }
            }
    }

推荐阅读