【问题标题】:How to use Gyroscope in swift如何快速使用陀螺仪
【发布时间】: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


    【解决方案1】:

    你说的地方

    let x = data.rotationRate.x
    let y = data.rotationRate.y
    let z = data.rotationRate.z
    

    您可以打印任何这些或使用标签

    例如,

    print("this is x:", x)
    print("this is y:", y)
    print("this is z:", z)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      相关资源
      最近更新 更多