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