gpt4 book ai didi

ios - 如何使用 Date() 函数和应用程序在前台和后台时的时间戳来获取耗时?

转载 作者:搜寻专家 更新时间:2023-11-01 07:00:52 24 4
gpt4 key购买 nike

我正在考虑使用 DataFormatter.localizedString... 并将值设置为 2 个不同的变量,一个用于应用程序离开前台和应用程序返回前台的时间,然后减去它们以获得耗时将其设置为 breakSession.text ...但我对 swift 有点陌生,不清楚如何使用 Date() 函数。我的主要目标是让 breakTimer 在应用程序处于后台时运行并在前台暂停时暂停,但用户仍然应该能够看到耗时。但显然,计时器无法在后台运行...有人可以帮忙吗?

import UIKit

class HomeViewController: UIViewController {

@IBOutlet weak var focusSession: UILabel!
@IBOutlet weak var breakSession: UILabel!

/** Focus Time **/

var prodMinutes = String() // Productive time data from ViewController.swift arrives and is stored
lazy var intProdSeconds = (60*Int(prodMinutes)!) + 1
var focusTimer = Timer()
var isFocusTimerRunning = false // Make sure only one timer is running at a time

/** Break Time **/

var breakMinutes = String() // Break time data from BreaTimeViewController.swift arrives and is stored
lazy var intBrSeconds = (60*Int(breakMinutes)!) + 1
var breakTimer = Timer()
var isBreakTimerRunning = false

override func viewDidLoad() {
super.viewDidLoad()

let state: UIApplicationState = UIApplication.shared.applicationState

/** Focus Time **/

if isFocusTimerRunning == false && state == .active {
runProdTimer()

}
else {
focusTimer.invalidate()
}

/** Break Time (This part doesn't work)

if isBreakTimerRunning == false && state == .background {
runBrTimer()
}
else {
breakTimer.invalidate()
} **/
}

func runProdTimer() {
focusTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(HomeViewController.updateProdTimer)), userInfo: nil, repeats: true)
isFocusTimerRunning = true
}

func runBrTimer() {
breakTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(HomeViewController.updateBrTimer)), userInfo: nil, repeats: true)
isBreakTimerRunning = true
}

@objc func updateProdTimer() {

if intProdSeconds < 1 {
focusTimer.invalidate()
focusSession.text = "00:00"
}
else {
intProdSeconds -= 1
focusSession.text = prodTimeString(fTime: TimeInterval(intProdSeconds))
}

}



@objc func updateBrTimer() {

if intBrSeconds < 1 {
breakTimer.invalidate()
breakSession.text = "00:00"
}

else {
intBrSeconds -= 1
breakSession.text = brTimeString(bTime: TimeInterval(intBrSeconds))
}

}

func prodTimeString(fTime: TimeInterval) -> String {

let prodMinutes = Int(fTime) / 60 % 60
let prodSeconds = Int(fTime) % 60

return String(format: "%02d:%02d", prodMinutes, prodSeconds)

}

func brTimeString(bTime: TimeInterval) -> String {

let brMinutes = Int(bTime) / 60 % 60
let brSeconds = Int(bTime) % 60

return String(format: "%02d:%02d", brMinutes, brSeconds)
}

更新:好的,下面 Sunil Chauhan 的回答有点奏效了。我能够得到时间戳和东西。但是,我遇到了另一个问题,我不明白为什么..

@objc func appWillEnterForeground() {
endDate = Date()
let secondsPassedInBackground = endDate.timeIntervalSince(beginDate)
print(secondsPassedInBackground)
let updateBrTime = intBrSeconds - Int(secondsPassedInBackground) // Updated Break Time after user returns to the app aka foreground
breakSession.text = brTimeString(bTime: TimeInterval(updateBrTime))
}

在这个函数中,我基本上想获取耗时并从 intBrSeconds 中减去它,这就像用户为自己设置的“休息”时间。所以从本质上讲,这就像一个计时器,因为当用户离开应用程序时,“Break Timer”就会运行。标签首先会正确更新,并根据耗时正确减去正确的秒数。然而,在耗时超过大约 30 秒左右之后,计时器开始增加。就像它被设置为 10:00 一样……它会下降到 9:34,但是下次您将应用程序留在后台并返回时,它会说 9:55 或大于 9 的时间: 34 这是不应该发生的。我该如何解决这个问题?

最佳答案

试试这个:

var beginDate = Date()
var endDate = Date()

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(appWentInBackground), name: .UIApplicationDidEnterBackground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: .UIApplicationWillEnterForeground, object: nil)
}

@objc
func appWentInBackground() {
beginDate = Date()
}

@objc
func appWillEnterForeground() {
endDate = Date()
let secondsPassedInBackground = endDate.timeIntervalSince(beginDate)
// secondsPassedInBackground passed while app was in background.
}

关于ios - 如何使用 Date() 函数和应用程序在前台和后台时的时间戳来获取耗时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51252211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com