Why am I not getting any accelerometer update?

拈花ヽ惹草 提交于 2019-12-12 02:03:27

问题


I am trying to get accelerometer updates with CoreMotion and Swift, here is what I placed in my viewDidLoad :

override func viewDidLoad() {
    super.viewDidLoad()
    let motionManager = CMMotionManager()
    motionManager.accelerometerUpdateInterval = 0.2
    motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()) {
        (info:CMAccelerometerData!,error:NSError!) in
        if error != nil {
            println(error)
        }
        else {
            println("OK")
        }
    }
}

The problem is that it looks like my closure never gets called (I don't have anything in the console), do you know why?


回答1:


The problem is that the variable motionManager, to which your CMMotionManager instance is assigned, is declared as a local variable (in the body of the function viewDidLoad), which means that it goes out of existence when the function finishes executing. Therefore its lifetime is about a 10000th of a second.

Well, that is not long enough for your CMMotionManager to obtain very many updates!



来源:https://stackoverflow.com/questions/27930175/why-am-i-not-getting-any-accelerometer-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!