我在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()
答案 0 :(得分:0)
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)")
}