swift - 检测 iPad 的方向 - Swift Playgrounds

标签 swift swift-playground

我在 Swift Playgrounds 中运行以下代码,但每次更改 iPad 的方向时,代码都会打印“无法确定状态”。我做错了什么还是有其他方法?

import UIKit
import PlaygroundSupport

class MainViewController: UIViewController {
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        switch UIDevice.current.orientation {
        case .portrait:
            print("Portrait")
        case .landscapeLeft:
            print("Landscape Left")
        case .landscapeRight:
            print("Landscape Right")
        case .portraitUpsideDown:
            print("Portrait Upside Down")
        default:
            print("Unable to Determine State")
        }
    }

}

PlaygroundPage.current.liveView = MainViewController()

最佳答案

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 4:

 override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    var text=""
    switch UIDevice.currentDevice().orientation{
    case .Portrait:
        text="Portrait"
    case .PortraitUpsideDown:
        text="PortraitUpsideDown"
    case .LandscapeLeft:
        text="LandscapeLeft"
    case .LandscapeRight:
        text="LandscapeRight"
    default:
        text="Another"
    }
    print("You have moved: \(text)")        
}

关于swift - 检测 iPad 的方向 - Swift Playgrounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49344530/

相关文章:

Swift:根据屏幕尺寸动态约束按钮尺寸

ios - 在 Swift 中使用 sysctlbyname()

ios - 格式化没有千位分隔符的双数?

swift - 计时器不在 Swift 3.0 Playground 上运行

swift - "Use of Unresolved Identifier"函数返回错误

ios - 如何在应用程序启动后立即加载插页式广告

swift - IOS8 TableViewController Pull to Refresh 无法添加 Action

ios - 如何在 Playground 中编辑描述(不是代码)

ios - 为什么我的按钮在 Swift Playgrounds 中不起作用?

swift - 为 RGB 图像创建灰度滤镜时,Swift 中的执行被中断