我正在尝试使用CMMotionManager来跟踪用户是跑步,走路还是开车。静止时,显示驾驶,表明它显示旧值。我该如何清除缓存,以便显示正确的内容,或者我的代码是否有问题,导致手机在静止时显示为自动:
这是我的代码:
//at beginning of class
let motionManager = CMMotionManager()
let motionActivityManager = CMMotionActivityManager()
//in viewdidload
if self.motionManager.isAccelerometerAvailable {
self.motionManager.accelerometerUpdateInterval = 0.01
motionActivityManager.startActivityUpdates(to: .main) { (activity) in
guard let activity = activity else {
return
}
if activity.walking {
self.rightBarButton.title = ""
}
else if activity.running {
self.rightBarButton.title = ""
}
else if activity.cycling {
self.rightBarButton.title = ""
}
else if activity.automotive {
self.rightBarButton.title = ""
}
else {
self.rightBarButton.title = "not moving"
}
}
}