我正在尝试使用属性UIDevice.currentDevice()。orientation,所以我有类似以下内容
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
var orientation = UIDevice.currentDevice().orientation
switch orientation {
case .Portrait:
print("portrait")
break
case .PortraitUpsideDown: // Device oriented vertically, home button on the top
print("upsidedown")
magneticHeading += 180
magneticHeading = magneticHeading % 360
break
case .LandscapeLeft: // Device oriented horizontally, home button on the right
print("left")
magneticHeading -= 90
magneticHeading = magneticHeading % 360
break
case .LandscapeRight: // Device oriented horizontally, home button on the left
print("right")
magneticHeading += 90
magneticHeading = magneticHeading % 360
break
case .FaceUp: // Device oriented flat, face up
print("up")
break
case .FaceDown: // Device oriented flat, face down
print("down")
break
case .Unknown:
print("unknown")
break
default:
print("default")
}
我浏览了很多有关Stack Overflow的网页和问题,所有这些都使用beginGeneratingDeviceOrientationNotifications(),然后它将起作用。但是,我的代码根本无法工作。在switch语句中,无论我如何转动手机,它始终输入.Portrait。有人知道为什么吗?
另外,我实际上想要的是设备的方向,而不是我的ViewController的方向。我需要知道设备的放置方式,所以我需要使用currentDevice()。
我也尝试使用以下方法:
var interfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
switch interfaceOrientation {
case .Unknown:
print("1")
case .Portrait:
print("2")
case .PortraitUpsideDown:
print("3")
case .LandscapeLeft:
print("4")
case .LandscapeRight:
print("5")
同样,无论我如何转动手机,都必须输入.Portrait
最佳答案