swift - 加速度计检测手机运动

标签 swift accelerometer

我正在尝试找出最准确的方法来检测手机何时向前移动并进行计数。到目前为止,我已经能够在每次加速度计 y 加速度超过某个两倍时进行计数,但这不是很准确,因为我要做的就是向前倾斜手机而不是让手机实际移动,从而使计数器上升。我怎样才能实现我想要的?我是否应该添加更多变量(例如 x 轴或 z 轴),还是需要在手机中加入陀螺仪或我当前未使用的其他功能?谢谢

这是我到目前为止的代码:

`override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        motionManager.accelerometerUpdateInterval = 0.1

        motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (accelerometerData: CMAccelerometerData?, error: NSError?) -> Void
            in

            self.outputAccelData(accelerometerData!.acceleration)
            if error != nil {

                print(error)

            }

        })
    func outputAccelData(acceleration: CMAcceleration) {


            if acceleration.y > 0.3 {

                countInt += 1

            }

            count.text = "\(countInt)"

        }

最佳答案

您可以使用 x、y 和 z 平方和的平方根来计算当前加速度大小。在静止状态下,无论手机的方向如何,该值都将保持不变(地球引力)。如果有人按下或拿起电话,那么这个幅度会暂时改变,您可以检测到该增量。您可能需要在检测之前稍微过滤这个幅度,具体取决于您尝试检测的运动有多小。

关于swift - 加速度计检测手机运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37053651/

相关文章:

iOS OpenGLES 内存泄漏

iphone - 如何在 iPhone 中使用 userAcceleration

c++ - 如何访问 Windows Mobile 设备的加速度计?

android - Android 加速度计的最小和最大范围是多少?

ios - Swift 中的 JSQMessagesViewController 与 Xcode 6.3 Objective-C 选择器问题

Swift - 无法检索 CMSensorDataList 记录

swift - 在swift中调用泛型函数时如何指定类型?

ios - MagicalRecord 不保存新对象

Android 传感器数据收集无法正常工作

iphone - 如何使用加速度计计算步数?