0

I am trying create navigation through the user shaking the iPhone to the side. However nothing seems to be working.

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
            }
        }

        }

I have imported Core Motion and Core Location and can't figure out whats wrong with it.

1 Answers1

-1

It's because motion manager must be instance of your class. See answer Motion Manager is not working in Swift. This should work

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
    }
}
Community
  • 1
  • 1
Daniel
  • 1
  • 1