检测 iPad 的方向 - Swift Playgrounds
Posted
技术标签:
【中文标题】检测 iPad 的方向 - Swift Playgrounds【英文标题】:Detect Orientation of iPad - Swift Playgrounds 【发布时间】:2018-03-18 04:44:07 【问题描述】:我在 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()
【问题讨论】:
您找到解决方案了吗?也遇到了同样的事情...... 【参考方案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 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 4 的代码没有更新。以上是关于检测 iPad 的方向 - Swift Playgrounds的主要内容,如果未能解决你的问题,请参考以下文章