【问题标题】:Swift Gyroscope快速陀螺仪
【发布时间】:2018-11-05 07:17:01
【问题描述】:

我正在构建一个需要拍摄关卡照片的应用。我想用陀螺仪来确保拍照时手机是水平的。

我已经获得了陀螺仪更新(即使设备没有移动也会逐渐增加),但我似乎不知道如何获得初始陀螺仪坐标。

如何获取陀螺仪的初始值?

这是我的代码的精简版

import UIKit
import CoreMotion

class SizingFlowFrontPhotoViewController: UIViewController, AVCapturePhotoCaptureDelegate {


    var gyroX:Double = 0.0
    var gyroY:Double = 0.0
    var gyroZ:Double = 0.0

    var motionManager = CMMotionManager()

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)


        motionManager.gyroUpdateInterval = 0.2
        motionManager.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
            if let myData = data {
                // x = rotate it on sim card (front flip/back flip)
                // y = rotate it on charger (twister spin)
                // z = rotate it on tilt (landscape vs portrait)
                self.gyroX += myData.rotationRate.x
                self.gyroY += myData.rotationRate.y
                self.gyroZ += myData.rotationRate.z
                print("gyroX: \(self.gyroX) - gyroY: \(self.gyroY) - gyroZ: \(self.gyroZ)")
            }
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        motionManager.stopGyroUpdates()
    }


}

【问题讨论】:

    标签: swift gyroscope


    【解决方案1】:

    我发现我使用了错误的设备。

    我应该一直在使用 deviceMotion。像这样。

    motionManager.deviceMotionUpdateInterval = 0.05
            motionManager.startDeviceMotionUpdates(to: OperationQueue.current!) { (data, error) in
                if let myData = data{
                    self.mPitch = myData.attitude.pitch * 180 / Double.pi
                    self.mRoll = myData.attitude.roll * 180 / Double.pi
                    self.mYaw = myData.attitude.yaw * 180 / Double.pi
                    self.updateGyroBarPosition()
                }
            }
    

    【讨论】:

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