Подтвердить что ты не робот

Как проверить, является ли ориентация устройства левым или правым в вертикальном направлении?

    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
        print("landscape")
    }
    if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
        print("portrait")
    }

Как я могу проверить, пейзаж ли он влево или вправо?

4b9b3361

Ответ 1

вы можете сделать что-то вроде:

if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortraitUpsideDown{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortrait{

}

SWIFT 3

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

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

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

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

        } 

Ответ 2

Это работает на Swift 3 & 4

switch UIApplication.shared.statusBarOrientation {
    case .portrait:
        //do something
        break
    case .portraitUpsideDown:
        //do something
        break
    case .landscapeLeft:
    //do something
        break
    case .landscapeRight:
        //do something
        break
    case .unknown:
        //default
        break
 }

Ответ 3

Есть одна вещь, которая уничтожает все эти ответы - это многозадачность iPad iOS9.

На iOS 9 iPad-приложение по умолчанию выбирает многозадачность для iPad. Это означает, что он должен принимать все ориентации во все времена. Поскольку вы не отказались от многозадачности iPad, среда выполнения предполагает, что вы все время придерживаетесь всех ориентаций - и, следовательно, вам не нужно беспокоиться о том, какие ориентации вы разрешаете, поскольку она уже знает ответ (все они).

Чтобы сделать правильный размер ячейки для UICollectionView, я делаю следующее:

func collectionView(_ collectionView: UICollectionView, layout 
    collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        if UIDevice.current.userInterfaceIdiom == .phone {
            return CGSize(width: collectionView.frame.size.width, height: 120)
        } else {
            var width:CGFloat = 0
            let height = 250
            // because of iOS9 and iPad multitasking
            if (UIApplication.shared.statusBarOrientation.rawValue <= UIInterfaceOrientation.portraitUpsideDown.rawValue) {
                width = (collectionView.frame.size.width - 3) / 3
            } else {
                width = (collectionView.frame.size.width - 4) / 4
            }
            return CGSize(width: Int(width), height: Int(height))
        }
    }

и внутри viewWillTransition next:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.invalidateLayout()
        }
}

потому что он работает быстрее, чем без.

Ответ 4

UIDeviceOrientation вернет значение для этого:

   enum UIDeviceOrientation : Int {
        case Unknown
        case Portrait
        case PortraitUpsideDown
        case LandscapeLeft
        case LandscapeRight
        case FaceUp 
        case FaceDown
    }

Ответ 5

Свифт 3+

switch UIDevice.current.orientation {
case .faceUp:
  print("flat - up")
case .faceDown:
  print("flat - down")
case .landscapeLeft:
  print("landscape - left")
case .landscapeRight:
  print("landscape - right")
case .portrait:
  print("portrait - normal")
case .portraitUpsideDown:
  print("portrait - upsideDown")
case .unknown:
  print("unknown")
}

Ответ 6

SWIFT 4 - Обновление доски от "Льва"

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
            print("Landscape Left")
        } 
else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight{
            print("Landscape Right")
        } 
else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown{
           print("Portrait Upside Down")
        } 
else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
            print("Portrait")
}

Ответ 7

SWIFT 4.2

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
                print("Landscape Left")
            } 
    else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight{
                print("Landscape Right")
            } 
    else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown{
               print("Portrait Upside Down")
            } 
    else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
                print("Portrait")
    }