首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当应用程序在前台和后台时,我如何使用Date()函数和时间戳来获取经过的时间?

当应用程序在前台和后台时,我如何使用Date()函数和时间戳来获取经过的时间?
EN

Stack Overflow用户
提问于 2018-07-10 02:48:43
回答 1查看 73关注 0票数 1

我在考虑用DataFormatter.localizedString..。并将值设置为两个不同的变量,一个用于应用程序何时离开前台和何时返回前台,然后减去它们以获得运行时间,将其设置为breakSession.text...but我对swift有点陌生,不知道如何使用Date()函数。我的主要目标是让breakTimer在应用程序在后台时运行,在应用程序在前台时暂停,但用户应该仍然能够看到经过的时间。但是很明显,定时器不能在background...Can中运行,有人能帮下忙吗?

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中减去,它就像用户为自己设置的“中断”时间。因此,从本质上讲,这是一个计时器,当用户离开应用程序时,“中断计时器”会运行。标签最初会正确更新,并根据经过的时间正确地减去正确的秒数。然而,在经过的时间超过大约30秒之后,计时器开始增加。所以,如果它被设置为10:00..it,它会下降到9:34,但是当你下一次离开应用程序后台回来时,它会说9:55或者比9:34更大的值,这不应该是happen..how,我可以修复这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-10 04:19:50

试试这个:

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.
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51252211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档