2016-04-26 17 views
0

iPhoneを横に振ってナビゲーションを作成しようとしています。しかし、何も働いていないようです。加速度計は登録されていませんか? - 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 
      } 
     } 

     } 

私はCore MotionとCore Locationをインポートしており、何が間違っているのか分かりません。

答えて

-1

モーションマネージャはあなたのクラスのインスタンスでなければならないからです。答えはMotion Manager is not working in Swiftです。 これは動作するはずです

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 
    } 
} 
関連する問題