2016-04-26 15 views
0

Ich versuche, Navigation durch den Benutzer schütteln das iPhone zur Seite zu schaffen. Nichts scheint zu funktionieren.Beschleunigungssensor wird nicht registriert? - Swift

override func viewDidLoad() { 
    super.viewDidLoad() 
    let manager = CMMotionManager() 
    if manager.deviceMotionAvailable { 
     manager.deviceMotionUpdateInterval = 0.02 
     manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) { 
      [weak self] (data: CMDeviceMotion?, error: NSError?) in 

      if data?.userAcceleration.x < -2.5 { 
       self!.firstView.hidden = true 
      } 
     } 

     } 

Ich habe Core Motion und Core Location importiert und kann nicht herausfinden, was falsch damit ist.

Antwort

-1

Es ist, weil Bewegungsmanager Instanz Ihrer Klasse sein muss. Siehe Antwort Motion Manager is not working in Swift. Dies sollte funktionieren

import CoreMotion 

class ViewController: UIViewController { 
    let motionManager: CMMotionManager = CMMotionManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     motionManager.deviceMotionUpdateInterval = 0.01 
     motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), 
     withHandler:{ 
      deviceManager, error in 
      println("Test") // no print 
     }) 

     println(motionManager.deviceMotionActive) // print false 
    } 
} 
Verwandte Themen