Nai*_*ian 10 ios core-motion cmmotionmanager swift
我尝试在Swift中使用运动管理器,但我的更新块中的日志从不打印.
var motionManager: CMMotionManager = CMMotionManager()
motionManager.accelerometerUpdateInterval = 0.01
println(motionManager.deviceMotionAvailable) // print true
println(motionManager.deviceMotionActive) // print false
motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler:{
deviceManager, error in
println("Test") // no print
})
println(motionManager.deviceMotionActive) // print false
Run Code Online (Sandbox Code Playgroud)
我的Objective-C实现工作正常.有谁知道为什么我的更新块没有被调用?
Mic*_*lum 24
那是因为当方法返回时,运动管理器实例被抛出.您应该在类上创建一个属性以包含运动管理器.此外,您似乎只是更改了管理器accelerometerUpdateInterval
,然后监视设备运动的变化.您应该设置deviceMotionUpdateInterval
属性.
import CoreMotion
class ViewController: UIViewController {
let motionManager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
motionManager.deviceMotionUpdateInterval = 0.01
motionManager.startDeviceMotionUpdates(to: OperationQueue.current!) { deviceManager, error in
print("Test") // no print
}
print(motionManager.isDeviceMotionActive) // print false
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12211 次 |
最近记录: |