私は計時用のストップウォッチアプリケーションを書いていますし、私はクロックが一定の時間間隔(午前20時00分のような - 6時)を超えているかどうかを確認する必要があり、その場合には、時間間隔に現在の経過時間を追加します私は後でデータベースに格納する変数です。どのように私はこれを達成するための任意のアイデアですか?これまではティック関数でチェックしようとしましたが、アプリがバックグラウンドになっている時間は追加されません。ストップウォッチのiOSスウィフト
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))
}
}
App Capabitiesで背景モードを有効にした後に試しましたか? –