如何访问和存储陀螺仪的y轴的正负值?

时间:2019-04-30 04:53:03

标签: swift gyroscope

我试图根据陀螺仪上“ y轴”的值来触发两个不同的选择。本质上,向上倾斜(在左侧横向)将触发“ Pointed Up” VC,其+ y 然后向下倾斜,即“ Pointed Down” VC,其值为-y。

一旦触发了这些VC,我需要停止陀螺仪更新,直到两个触发的VC中的任何一个在各自的延迟后都已释放为止。

然后,随后重新启动陀螺仪更新。

我试图创建一个if / else语句,并在var中表示这些值并返回它,但是当我尝试调用返回值时,出现错误“未调用'startGyroScope()'的结果”错误

const x = getter<Point>(p)('x');

...

//Declared just after class is initialised
var correctAnswer = true
var motionManager = CMMotionManager()

...

override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        startGyroScope()
        let answerGiven = startGyroScope()
        performAnswerSegue(triggeredAnswer: answerGiven)   
    }

...

    func startGyroScope() -> Bool {

        var pickedAnswer: Bool = true

        motionManager.gyroUpdateInterval = 0.25
        motionManager.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
            if let gyroData = data{

            let y = gyroData.rotationRate.y

                            //User tilted for true
                            if y > 2 {
                                pickedAnswer = true
                                self.motionManager.stopGyroUpdates()
                                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
                                    self.motionManager.startGyroUpdates()
                                }

                            }

                                //User tilted for false
                            else if y < -2 {
                                pickedAnswer = false
                                self.motionManager.stopGyroUpdates()
                                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
                                    self.motionManager.startGyroUpdates()
                                }

                            }
                }

            }
        return pickedAnswer
}

gyro Storyboard

0 个答案:

没有答案