Определить альбомную ориентацию влево или вправо - PullRequest
0 голосов
/ 28 апреля 2020

Мое приложение поддерживает книжную и альбомную ориентацию -> как слева, так и справа. Я могу обнаружить, если его ландшафт. Но не в состоянии обнаружить слева или справа. Вот мой код

if UIDevice.current.orientation.isLandscape {
// Do some task 
}

Когда пользователь поворачивает устройство, мне нужно определить, повернулся ли пользователь в горизонтальную или левую ориентацию!

Точно так же, как в моем вышеупомянутом состоянии, мне нужно проверить, его левая или правая сторона. Как я могу это обнаружить?

Спасибо

1 Ответ

0 голосов
/ 28 апреля 2020

Я думаю, что вы ищете что-то вроде этого

    if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {


    } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {

    } else {
        //not landscape left or right
    }

РЕДАКТИРОВАТЬ --------

на основе ваших комментариев вы ищете ориентацию интерфейса вместо ориентации устройства ,

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    var text=""
    switch UIDevice.current.orientation{
    case .portrait:
        text="Portrait"
    case .portraitUpsideDown:
        text="PortraitUpsideDown"
    case .landscapeLeft:
        text="LandscapeLeft"
    case .landscapeRight:
        text="LandscapeRight"
    default:
        text="Another"
    }
    NSLog("You have moved: \(text)")        
}

приведенный выше код определяет ориентацию интерфейса ... обратите внимание, что оператор switch все еще использует UIDeviceOrientation

Ниже приведен еще один метод, который вы можете использовать

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape {
        print("landscape")
    } else {
        print("portrait")
    }
}

Обратите внимание, что UIDevice Orientation все еще используется ...

Ниже приведен очень другой, но эффективный подход.

struct DeviceInfo {
struct Orientation {
    // indicate current device is in the LandScape orientation
    static var isLandscape: Bool {
        get {
            return UIDevice.current.orientation.isValidInterfaceOrientation
                ? UIDevice.current.orientation.isLandscape
                : UIApplication.shared.statusBarOrientation.isLandscape
        }
    }
    // indicate current device is in the Portrait orientation
    static var isPortrait: Bool {
        get {
            return UIDevice.current.orientation.isValidInterfaceOrientation
                ? UIDevice.current.orientation.isPortrait
                : UIApplication.shared.statusBarOrientation.isPortrait
        }
    }
}}
...