受Nike Running应用程序的启发,我想知道当应用程序处于后台/最小化状态时,如何在Xcode控制台中获得陀螺仪更新。下面提到的代码工作得很好,并且非常顺利地显示了轴的读数。
override func viewDidLoad() {
super.viewDidLoad()
let motionManager = CMMotionManager()
motionManager.gyroUpdateInterval = 0.1
motionManager.startGyroUpdates()
if motionManager.deviceMotionAvailable {
motionManager.deviceMotionUpdateInterval = 0.01
motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (data: CMDeviceMotion?, error: NSError?) -> Void in
let x = data!.gravity.x
let y = data!.gravity.y
let z = data!.gravity.z
print(x)
self.view.backgroundColor = UIColor(
red: CGFloat(abs(x)),
green: CGFloat(abs(y)),
blue: CGFloat(abs(z)),
alpha: 1.0)
})
}
}