2016-06-19 11 views
5

Ich versuche das Gerät Bewegung (Pitch, Roll, Gieren), aber meine Funktion handleDeviceMotionUpdate ist nicht gestartet (Ich versuche es auf einem iPhone 5s), hier ist der Code:Motion Manager funktioniert nicht

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)") 

} 



} 

Ich bekomme den Fehler in startDeviceMotionUpdatesToQueue nicht. Ich bekomme true auf motionManager.deviceMotionActive und auf motionManager.deviceMotionAvailable

Antwort

6

Ihr MotionManager wird wahrscheinlich zerstört, nachdem es die ViewDidLoad Methode beendet. Versuchen Sie, MotionManager zu einer Klasseneigenschaft zu machen.

class ViewController: UIViewController { 
    var motionManager = CMMotionManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.motionManager.startDeviceMotionUpdates() 

     ... 
+0

oh danke, es hat funktioniert! – Theilya