1
シンプルだけど苦労しています...下記のコードはworkoutTimerの日付を設定する行でクラッシュします。また、私のWKInterfaceTimerはIBOutletに接続されていません。私は時間の目的のためだけに使用したかったのです。Swift 4でWatchOSでシンプルタイマーを作成
class InterfaceController {
var workoutTimer: WKInterfaceTimer!
var workoutStartTime: NSDate? = nil
func startWorkOutTimer() {
self.startWorkout()
if let test = self.workoutSecondsElapsed() {
print("timer seconds = \(test)")
}
}
func startWorkout() {
// To count up use 0.0 or less, otherwise the timer counts down.
workoutTimer.setDate(NSDate(timeIntervalSinceNow: 0.0) as Date)
workoutTimer.start()
self.workoutStartTime = NSDate()
}
func stopWorkout() {
workoutTimer.stop()
}
func workoutSecondsElapsed() -> TimeInterval? {
// If the timer hasn't been started then return nil
guard let startTime = self.workoutStartTime else {
return nil
}
// Time intervals from past dates are negative, so
// multiply by -1 to get the elapsed time.
return -1.0 * (self.workoutStartTime?.timeIntervalSinceNow)!
}
}
「Timer」を使用してくれてありがとう、私のように 'WKInterfaceTimer'をハックしようとするよりもずっと簡単です。 – GarySabo
あなたは歓迎です;) –