Swift2 : NSTimerをバックグラウンドで10分以上の動作
上記質問の、
バックグラウンドに移行したタイミングの時間と、
アプリを再度動作させた時の時間を計算して、
表示させるため、NSDateでバックグラウンドに移行した
タイミングの時間と、フォアグラウンドに移行したタイミングの時間を取得したのですが、差分をどうやってとればいいのか悩んでいます。
String型で取得したり、いろいろ考えましたが、いい案が思いつきません。
何か方法がありましたら、教えていただけると幸いです。
カウントダウンの実装は以下のようになっています。
差分をtimerCountから引いて、表示したいと思っています。
var timerCount: Int = 1000 timerCount -= 1 timerlabel.text = "\(timerCount / 3600): \(timerCount / 60 % 60) : \(timerCount % 60) "
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ベストアンサー
やりたいことはこんな感じで動くと思います、コピペして試してみてください。
コンソールに時間が出ます。
swift
1 2import UIKit 3 4class ViewController: UIViewController { 5 6 var backgroundDate: NSDate! 7 var timerCount: Int = 0 8 var timer: NSTimer = NSTimer() 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 addNotification() 14 15 // 20秒に設定 16 timerCount = 20 17 startTimer() 18 } 19 20 func addNotification() { 21 // 通知の登録 22 NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive:", name: UIApplicationDidBecomeActiveNotification, object: nil) 23 24 NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground:", name: UIApplicationDidEnterBackgroundNotification, object: nil) 25 } 26 27 override func viewDidDisappear(animated: Bool) { 28 super.viewDidDisappear(animated) 29 // 通知の削除 30 NSNotificationCenter.defaultCenter().removeObserver(self) 31 } 32 33 // フォアグラウンドになった時に呼ばれる 34 func didBecomeActive(notify: NSNotification) { 35 36 if let backgroundDate = backgroundDate where timerCount > 0 { 37 38 // バックグラウンドに入った時間とフォアグラウンドになった時間の差分を取得 ※簡易的にIntにキャストしています。 39 let timeDiff = Int(NSDate().timeIntervalSinceDate(backgroundDate)) 40 print("経過時間 : \(timeDiff)") 41 42 if timeDiff < timerCount { 43 44 // 経過時間よりタイマーの残り時間が多かった場合、再度タイマースタート 45 timerCount -= timeDiff 46 startTimer() 47 } 48 } 49 } 50 51 // バックグラウンドに入った時に呼ばれる 52 func didEnterBackground(notify: NSNotification) { 53 54 // タイマー停止 55 timer.invalidate() 56 // バックグラウンドに入った時間を保持 57 backgroundDate = NSDate() 58 } 59 60 func startTimer() { 61 62 if timer.valid { 63 timer.invalidate() 64 } 65 66 // timerを生成する. 67 timer = NSTimer(timeInterval: 1, target: self, selector: "countDown:", userInfo: nil, repeats: true) 68 NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) 69 } 70 71 72 func countDown(timer: NSTimer){ 73 74 timerCount-- 75 print("Timer Count Down : \(timerCount)") 76 77 if timerCount <= 0 { 78 timer.invalidate() 79 } 80 } 81 82 override func didReceiveMemoryWarning() { 83 super.didReceiveMemoryWarning() 84 } 85} 86
投稿2015/12/08 13:42
総合スコア8490
0
すみません、実際に試せていません。
下記ページの実装例が参考になりませんか?
[NSTimerでStopWatchを作る](https://akira-watson.com/iphone/apple-watch-timer.html)
swift
1 // 現時間 2 let currentTime = NSDate.timeIntervalSinceReferenceDate() 3 // 開始時間と現時間の差分を取る 4 var diffTime: NSTimeInterval = currentTime - startTime!
つまり、diffTime: NSTimeInterval
を使えば時刻の差分を取得出来るのではないでしょうか?
投稿2015/12/08 13:05
総合スコア5936
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/09 02:48