Ios 当应用程序位于前台和后台时,如何使用Date()函数和时间戳来获取经过的时间?
我在考虑使用DataFormatter.localizedString。。。将该值设置为两个不同的变量,一个用于应用程序离开前台时,另一个用于应用程序返回前台时,然后减去它们以获得将其设置为breakSession.text所用的时间…但我对swift有点陌生,不清楚如何使用Date()函数。我的主要目标是让breakTimer在应用程序位于后台时运行,在应用程序位于前台时暂停,但用户应该仍然能够看到经过的时间。但显然,计时器不能在后台运行…有人能帮忙吗Ios 当应用程序位于前台和后台时,如何使用Date()函数和时间戳来获取经过的时间?,ios,swift,timer,nsdate,state,Ios,Swift,Timer,Nsdate,State,我在考虑使用DataFormatter.localizedString。。。将该值设置为两个不同的变量,一个用于应用程序离开前台时,另一个用于应用程序返回前台时,然后减去它们以获得将其设置为breakSession.text所用的时间…但我对swift有点陌生,不清楚如何使用Date()函数。我的主要目标是让breakTimer在应用程序位于后台时运行,在应用程序位于前台时暂停,但用户应该仍然能够看到经过的时间。但显然,计时器不能在后台运行…有人能帮忙吗 import UIKit class
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)
}
在这个函数中,我基本上想要获取经过的时间,并从intBrSeconds中减去它,这就像用户为自己设置的“中断”时间。因此,本质上,这是一个计时器,当用户离开应用程序时,“中断计时器”会运行。标签首先正确更新,并根据经过的时间正确减去正确的秒数。但是,在经过的时间超过大约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.
}
那么secondsPassedInBackground将是我的耗用时间值,作为一个整数?这是秒耗用时间值,作为双精度。根据我的说法,这应该行得通。@这个答案显示了我所说的观察者,它们是检测应用程序进入后台或前台的方法。好的,Sunil,我在上面发布了一个更新^你能检查一下吗?!你的方法在某种程度上起了作用,但我仍然面临一些其他问题。嘿@Chris你能看看我上面的更新吗^。。。我仍然有一些问题。根据你更新的^info,听起来你不需要知道在后台花了多少时间,你只需要知道从现在到我们开始跟踪休息时间已经花了多少时间,不管应用程序是在后台还是前台,对吗?@Augie不,对不起,我需要应用程序离开前台和返回前台之间经过的时间。然后,我从intBrSeconds中减去经过的时间并更新标签。我需要每次应用程序离开前台并返回时都发生这种情况,这样标签每次都会更新,并显示为“计时器”。我不能使用timer()来完成此操作,因为显然计时器不能在应用程序的后台运行。
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.
}