swift为后台运行的计时器计算时间
我想运行2个可互换的计时器,但我希望他们“运行”,即使应用程序是在后台 理想的行为是,用户启动计时器,进入后台,等待可更换计时器触发的通知。。。计时器可以交换很多次。当用户将应用程序带回前台时。。他想知道当前计时器的准确时间 在iOS模拟器中,计时器在后台工作,但在设备上不工作 根据我所附的图片,我试图计算当前计时器到期的时间X,并根据结束时间获取当前时间。守则如下:swift为后台运行的计时器计算时间,swift,timer,background,Swift,Timer,Background,我想运行2个可互换的计时器,但我希望他们“运行”,即使应用程序是在后台 理想的行为是,用户启动计时器,进入后台,等待可更换计时器触发的通知。。。计时器可以交换很多次。当用户将应用程序带回前台时。。他想知道当前计时器的准确时间 在iOS模拟器中,计时器在后台工作,但在设备上不工作 根据我所附的图片,我试图计算当前计时器到期的时间X,并根据结束时间获取当前时间。守则如下: func appDidBecomeActive(){ // TODO: calculate time spend in bac
func appDidBecomeActive(){
// TODO: calculate time spend in background
isInBackground = false
if let startTime = startTime{
let now = NSDate()
var timeFromBegining = now.timeIntervalSinceDate(startTime)
while timeFromBegining > userDefaultsHelper.timerAseconds(){
timeFromBegining -= userDefaultsHelper.timerAseconds()
timerType = .tiemrTypeB
let stopTimeSeconds = userDefaultsHelper.timerBseconds() - timeFromBegining
stopTime = NSDate().dateByAddingTimeInterval(stopTimeSeconds)
print("Time from Begining: \(timeFromBegining)")
if timeFromBegining > userDefaultsHelper.timerBseconds(){
timeFromBegining -= userDefaultsHelper.timerBseconds()
timerType = .timerTypeA
// TODO: do something with the remaining time ...
let stopTimeSeconds = userDefaultsHelper.timerAseconds() - timeFromBegining
stopTime = NSDate().dateByAddingTimeInterval(stopTimeSeconds)
print("Time from Begining: \(timeFromBegining)")
}
}
}
}
个人而言,我认为A和B的组合持续时间是一个“周期”。然后,当应用程序重新启动时,我将进行模数计算,以确定(a)自计时器a第一次启动以来,已经过了多少个周期;和(b)计算我们在当前周期内的位置。由此,您可以轻松计算每个计时器下次出现之前的剩余时间:
let elapsed = Date().timeIntervalSince(startTime)
let totalDuration = durationOfTimerA + durationOfTimerB
let whereInCycle = elapsed.remainder(dividingBy: totalDuration)
let cycleCount = Int(elapsed / totalDuration)
var timeUntilNextA: Double
var timeUntilNextB: Double
if whereInCycle < durationOfTimerA {
timeUntilNextA = durationOfTimerA - whereInCycle
timeUntilNextB = timeUntilNextA + durationOfTimerB
} else {
timeUntilNextB = durationOfTimerA + durationOfTimerB - whereInCycle
timeUntilNextA = timeUntilNextB + durationOfTimerA
}
if cycleCount < 1 {
if whereInCycle < durationOfTimerA {
print("timer A hasn't fired yet")
} else {
print("timer A has fired, but B hasn't")
}
} else {
if whereInCycle < durationOfTimerA {
print("both timers A and B have fired \(cycleCount) times, and waiting for A to fire next")
} else {
print("timers A has fired \(cycleCount+1) times, but B has fired only \(cycleCount) times; we waiting for B to fire next")
}
}
print("A will fire in \(timeUntilNextA) seconds; B will fire in \(timeUntilNextB)")
let appeased=Date().timeIntervalSince(startTime)
设totalDuration=durationOfTimerA+durationOfTimerB
让whereInCycle=appead.rements(dividingBy:totalDuration)
让cycleCount=Int(已用/总持续时间)
var timeUntilNextA:Double
var timeUntilNextB:Double
如果周期<持续时间{
timeUntilNextA=时间的持续时间-其中周期
timeUntilNextB=timeUntilNextA+timerb的持续时间
}否则{
timeUntilNextB=timera的持续时间+timerb的持续时间-其中循环
timeUntilNextA=timeUntilNextB+timera持续时间
}
如果循环计数<1{
如果周期<持续时间{
打印(“计时器A尚未启动”)
}否则{
打印(“计时器A已启动,但B未启动”)
}
}否则{
如果周期<持续时间{
打印(“计时器A和B都已触发\(循环计数)次,并等待A下一次触发”)
}否则{
打印(“计时器A已触发\(cycleCount+1)次,但B仅触发\(cycleCount)次;我们正在等待B下一次触发”)
}
}
打印(“A将在\(timeUntilNextA)秒内触发;B将在\(timeUntilNextB)秒内触发”)
对于简单的一瞥来说,这看起来太多了。把你的问题简化为最基本的必需品。@GrantPark-FYI,这是对的后续问题。@Rob!我真是太感谢你了!简单而优雅的解决方案。