万歩計アプリを制作中です。
以下のコードだと自分が静止している時にも時間がカウントされて困っています。
歩いている時にのみ時間をカウントして、静止している時はカウントを止めるにはどうしたら良いでしょうか?よろしくお願いします。
Swift
1 myPedometer.startUpdates(from: Date(), withHandler: {(pedometerData, error) in 2 if let e = error { 3 print(e.localizedDescription) 4 return 5 } 6 guard let data = pedometerData else { 7 return 8 } 9 10 let time = Int(data.endDate.timeIntervalSince(data.startDate)) 11 let hh = Int(time / 3600) 12 let mm = Int((time - Int(hh * 3600)) / 60) 13 let ss = Int(time - Int(hh * 3600 + mm * 60)) 14 15 DispatchQueue.main.async { 16 print("isMainThread=", Thread.isMainThread) 17 //歩数を表示 18 self.stepLabel.text = "\(data.numberOfSteps) step" 19 20 //時間を表示 21 self.timeLabel.text = String(format: "%02d:%02d:%02d", hh, mm, ss) 22 23 } 24 25 })
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。