应用程序在 CMMotionManager 回调 'startDeviceMotionUpdatesToQueue' 时崩溃

app crashed when 'startDeviceMotionUpdatesToQueue' of CMMotionManager callback

在我的项目中,我使用了CoreMotion,我的代码在img中。有时它崩溃了,但我不能重复,我也不知道为什么..有人可以帮助我吗?我被它折磨了好久...

启动运动管理器:

Please try this code

    import CoreMotion

    var motionManager: CMMotionManager?

        override func viewDidLoad() {
                super.viewDidLoad()
                motionManager = CMMotionManager()
                if motionManager?.isDeviceMotionAvailable == true {

                    motionManager?.deviceMotionUpdateInterval = 0.1;

                    let queue = OperationQueue()
                    motionManager?.startDeviceMotionUpdates(to: queue, withHandler: { [weak self] (motion, error) -> Void in

                        // Get the attitude of the device
                        if let attitude = motion?.attitude {
                            // Get the pitch (in radians) and convert to degrees.
                            // Import Darwin to get M_PI in Swift
                            print(attitude.pitch * 180.0/M_PI)
                            }
                    })

                    print("Device motion started")
                }
                else {
                    print("Device motion unavailable");
                }
                // Do any additional setup after loading the view, typically from a nib.
            }


based on attitude.pitch you can know Device angle and rotation on ( + & - )