我在 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/