我想知道如何使用swift
检测iPhone是否在过去60秒内移动
我在下面看到了这个代码,但只有当手机摇晃时才有效
当手机移动但没有剧烈摇晃时,是否可以检测到?
override func motionEnded(motion: UIEventSubtype,
withEvent event: UIEvent?) {
if motion == .MotionShake {
let controller = UIAlertController(title: "Shake",
message: "The device is shaken",
preferredStyle: .Alert)
controller.addAction(UIAlertAction(title: "OK",
style: .Default,
handler: nil))
presentViewController(controller, animated: true, completion: nil)
}
}
我认为您需要使用加速度计并对输出应用中等低通滤波器,这样您就可以忽略非常缓慢的变化,但可以获得中等的加速度。您应该能够调整阈值。
我建议搜索"ios加速度计低通滤波器"。你需要编写自己的代码。