简体   繁体   中英

Cannot convert value of type '(CMDeviceMotion?, NSError?) -> ()' to expected argument type 'CMDeviceMotionH

I recently upgraded my xcode. i am currently using 9.2 I am using CMMotionManger and this error shows in new version. I tried solving it but could not find the solution.

func startCameraTracking() {
        motionManager.deviceMotionUpdateInterval = 1.0 / 60.0


        motionManager.startDeviceMotionUpdatesToQueue(OperationQueue.main) {
            [weak self](data: CMDeviceMotion?, error: NSError?) in

            guard let data = data else { return }

            let attitude: CMAttitude = data.attitude
            self?.cameraNode.eulerAngles = SCNVector3Make(Float(attitude.roll + M_PI/2.0), -Float(attitude.yaw), -Float(attitude.pitch))
        }
    }

As suggested by Michael, you should better check the latest reference:

( startDeviceMotionUpdatesToQueue(_: withHandler:) is renamed to startDeviceMotionUpdates(to:withHandler:) .)

startDeviceMotionUpdates(to:withHandler:)

Declaration

 func startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler) 

CMDeviceMotionHandler

Declaration

 typealias CMDeviceMotionHandler = (CMDeviceMotion?, Error?) -> Void 

For the second parameter of startDeviceMotionUpdates(to:withHandler:) , you need to pass a closure taking CMDeviceMotion? and Error? , not NSError? :

func startCameraTracking() {
    motionManager.deviceMotionUpdateInterval = 1.0 / 60.0

    motionManager.startDeviceMotionUpdates(to: OperationQueue.main) {
        [weak self](data: CMDeviceMotion?, error: Error?) in

        guard let data = data else { return }

        let attitude: CMAttitude = data.attitude
        self?.cameraNode.eulerAngles = SCNVector3Make(Float(attitude.roll + .pi/2.0), -Float(attitude.yaw), -Float(attitude.pitch))
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM