繁体   English   中英

如何检测手机是否被移动

[英]how to detect if phone has been moved


我想知道如何使用 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加速计低通滤波器”。 您将需要编写自己的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM