【发布时间】:2020-04-28 15:49:11
【问题描述】:
我正在尝试在我的项目中使用苹果提供的代码将陀螺仪数据记录到我可以使用的变量中。目前,这就是我必须要做的。我不确定如何将陀螺仪数据复制到可以在此类之外使用的变量中,甚至可以打印该值。我不确定它是否开始了。任何帮助将不胜感激。
import Foundation
import UIKit
import CoreMotion
class ViewController : UIViewController {
let motion = CMMotionManager()
var timer = Timer()
var GyroVar = 0
var gyroData: CMGyroData?
func startGyros() {
if motion.isGyroAvailable {
self.motion.gyroUpdateInterval = 1.0 / 60.0
self.motion.startGyroUpdates()
// Configure a timer to fetch the accelerometer data.
self.timer = Timer(fire: Date(), interval: (1.0/60.0),
repeats: true, block: { (timer) in
// Get the gyro data.
if let data = self.motion.gyroData {
let x = data.rotationRate.x
let y = data.rotationRate.y
let z = data.rotationRate.z
print("gyro works")
// Use the gyroscope data in your app.
}
print("outloop")
})
// Add the timer to the current run loop.
RunLoop.current.add(self.timer, forMode: RunLoop.Mode.default)
}
}
func stopGyros() {
print("stop")
if self.timer != nil {
self.timer.invalidate()
self.motion.stopGyroUpdates()
}
}
}
【问题讨论】:
标签: ios swift xcode gyroscope core-motion