gpt4 book ai didi

ios - 秒表 iOS Swift

转载 作者:行者123 更新时间:2023-11-28 12:14:52 28 4
gpt4 key购买 nike

我正在编写一个用于计时的秒表应用程序,我需要检查时钟是否超过某个时间间隔(例如 20:00 - 06:00),在这种情况下,将当前耗时添加到我设置的 TimeInterval 变量中稍后将存储在数据库中。关于如何实现这一目标的任何想法?到目前为止,我已经尝试在滴答功能中检查它,但是当应用程序处于后台时它不会添加时间。

    import Foundation
import os.log

/// The class protocol for a Stopwatch instance.
protocol StopwatchDelegate: class {
func currentTimerUpdated(seconds: String, minutes: String, hours: String)
func timerHasStarted()
func timerHasStopped()
func timerWasReset()
}

/// A container for a stopwatch.
class Stopwatch{

//var currentTimerSession = [TimerSession] = []

// *****************************************************************
// MARK: - Stopwatch Properties
// *****************************************************************

/// The middleman for communicating with the view controller.
weak var delegate: StopwatchDelegate?

/// The actual Timer for this stopwatch.
private(set) var timer: Timer?

/// The time at which the timer was started.
private(set) var timerStartTime: TimeInterval = 0

/// The Timer-time at which the last user *pause* occurred.
private(set) var timerSavedTime: TimeInterval = 0

private(set) var timerSession = false

// *****************************************************************
// MARK: - Stopwatch Class Methods
// *****************************************************************
/// Create a new Stopwatch by locating the middleman and starting the timer.
///
/// - Parameter delegate: the middleman to communicate to the view controller.
/// - Returns: An instance to a ticking `Stopwatch`.
///
init(delegate: StopwatchDelegate) {
os_log("Initilzing timer.", log: OSLog.default, type: .debug)
self.delegate = delegate
}

// *****************************************************************
// MARK: - Stopwatch Timer Methods
// ***************************************************************** *****************************************************************

/// Start the stopwatch's timer.
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self,
selector: #selector(timerHasTicked(timer:)),
userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: RunLoopMode.commonModes)

/// Record the time at which we started this timer.
//checkForOngoingTimer()
timerStartTime = Date.timeIntervalSinceReferenceDate

timerOriginalStartTime = Date()
timerSession = true


delegate?.timerHasStarted()
}

/// Pause the Timer
func stopTimer() {
/// Save the Time delta of the current Timer.
let currentTime = Date.timeIntervalSinceReferenceDate
timerSavedTime += currentTime - timerStartTime
timer?.invalidate()
delegate?.timerHasStopped()
}

/// Reset the Timer and all of the laps.
func resetTimer() {
timerSavedTime = 0
timerStartTime = 0
timerSession = false

timer?.invalidate()
delegate?.timerWasReset()
}

/// Compute the new time values time values:
///
/// - `minutes`: starts at 0 and increments to 59
/// - `seconds`: starts at 0 and increments to 59
/// - `hours`: starts at 0 and increments to 59
///
/// - Parameter timer: The instance of the currently *running* Timer.
///
@objc private func timerHasTicked(timer: Timer) {

/// Find the time delta between start time and now.
let currentTime = Date.timeIntervalSinceReferenceDate
let timerElapsedTime: TimeInterval = (currentTime - timerStartTime) + timerSavedTime

/// Convert elapsed time to minutes, seconds, and hours.
let minutes = Int(timerElapsedTime) / 60 % 60
let seconds = Int(timerElapsedTime) % 60
let hours = Int(timerElapsedTime / 3600)

let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "yyyy-MM-dd HH:mm"
timerOriginalStartTime = formatter.string(from: timerOriginalStartTime)

/// Let the delegate know the Time has been updated.
delegate?.currentTimerUpdated(seconds: String(format: "%02u", seconds),
minutes: String(format: "%02u", minutes),
hours: String(format: "%02u", hours))
}
}

最佳答案

你的计时器不会在后台触发,所以最好的方法是在你启动秒表时存储开始时间,然后当应用程序进入后台并再次返回时,你可以计算当前与开始时间的偏移量。

如果你想支持暂停计时器,你可以跟踪暂停的开始和停止时间,并从总时间中减去它。

关于ios - 秒表 iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47070296/

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