How to know device is tilting to left/right/backward or front using CMMotionManager

371 Views Asked by At

I need to identify which way iOS device is tilting, left, right, forward, backward.

I am trying to identify this with below code but not working.

self.motionManager.isDeviceMotionAvailable {
    self.motionManager.deviceMotionUpdateInterval = 1
    self.motionManager.startDeviceMotionUpdates(to: OperationQueue()) { [weak self] (motion, error) -> Void in
        if let attitude = motion?.attitude {
            if attitude.pitch > 0{
                print("forward");
            }else if attitude.pitch <= 0{
                print("backward");
            }
            else if (attitude.roll <= -1){
                print("left");
            }else if(attitude.roll > 1){
                print("roght");
            }
            DispatchQueue.main.async{
                // Update UI
            }
        }
    }
    print("Device motion started")
}
0

There are 0 best solutions below