为什么我没有得到任何加速度计更新?

时间:2015-01-13 19:39:50

标签: ios swift core-motion

我正在尝试使用CoreMotion和Swift获取加速计更新,这是我放在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")
        }
    }
}

问题是看起来我的闭包永远不会被调用(我在控制台中没有任何东西),你知道为什么吗?

1 个答案:

答案 0 :(得分:3)

问题是您的CMMotionManager实例所分配到的变量motionManager被声明为局部变量(在函数viewDidLoad的主体中),这意味着它已经离开函数完成执行时存在。因此它的寿命大约是10000秒。

嗯,这不足以让你的CMMotionManager获得很多更新!

相关问题