Мне нужно определить, в каком направлении iOS устройство наклоняется, влево, вправо, вперед, назад.
Я пытаюсь идентифицировать это с помощью приведенного ниже кода, но не работает.
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")
}