我正在尝试使用属性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()。orientation
我也尝试过使用以下内容:
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