Как узнать, что устройство наклоняется влево / вправо / назад или вперед, используя CMMotionManager - PullRequest
1 голос
/ 29 апреля 2020

Мне нужно определить, в каком направлении 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")
}
...