在Swift中获取设备方向 [英] Getting device orientation in Swift
本文介绍了在Swift中获取设备方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在Swift中获取当前的设备方向?我知道有一些Objective-C的例子,但我无法让它在Swift中运行。
I was wondering how I can get the current device orientation in Swift? I know there are examples for Objective-C, however I haven't been able to get it working in Swift.
我正在尝试获取设备方向并将其放入进入if语句。
I am trying to get the device orientation and put that into an if statement.
这是我遇到的问题最多的一行:
This is the line that I am having the most issues with:
[[UIApplication sharedApplication] statusBarOrientation]
推荐答案
你可以使用:
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"
}
NSLog("You have moved: \(text)")
}
SWIFT 3更新
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
var text=""
switch UIDevice.current.orientation{
case .portrait:
text="Portrait"
case .portraitUpsideDown:
text="PortraitUpsideDown"
case .landscapeLeft:
text="LandscapeLeft"
case .landscapeRight:
text="LandscapeRight"
default:
text="Another"
}
NSLog("You have moved: \(text)")
}
或
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
通知您可以查看: IOS8 Swi ft:如何检测方向变化?
注意: didRotateFromInterfaceOrientation 已弃用对于iOS 2.0及更高版本使用
viewWillTransitionToSize
NOTE : didRotateFromInterfaceOrientation is Deprecated Use viewWillTransitionToSize for iOS 2.0 and later
这篇关于在Swift中获取设备方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文