Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

motion manager not working

I'am trying to get the device motion (pitch,roll,yaw) but my function handleDeviceMotionUpdate isn't launched (I'am trying it on an iphone 5s) , here is the code :

import UIKit
import CoreMotion

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()






    var motionManager = CMMotionManager()
    motionManager.startDeviceMotionUpdates()
    motionManager.deviceMotionUpdateInterval = 0.1


    motionManager.startDeviceMotionUpdatesToQueue(
        NSOperationQueue.currentQueue()!, withHandler: {
            (deviceMotion, error) -> Void in

            if(error == nil) {
                self.DeviceMotionUpdate(deviceMotion!)
            } else {
                print("error")
            }
    })

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func DeviceMotionUpdate(deviceMotion:CMDeviceMotion) {
    print("function launched")
    var attitude = deviceMotion.attitude
    var roll = (attitude.roll)
    var pitch = (attitude.pitch)
    var yaw = (attitude.yaw)
    print("Roll: \(roll), Pitch: \(pitch), Yaw:  (yaw)")

}



}

I don't get the error in startDeviceMotionUpdatesToQueue . I get true on motionManager.deviceMotionActive and on motionManager.deviceMotionAvailable

like image 241
Theilya Avatar asked Jun 19 '16 15:06

Theilya


1 Answers

Your motionManager probably gets destroyed after it finished running the viewDidLoad method. Try making motionManager a class property.

class ViewController: UIViewController {
    var motionManager = CMMotionManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.motionManager.startDeviceMotionUpdates()

        ...
like image 188
Jan Doornbos Avatar answered Nov 01 '22 10:11

Jan Doornbos