设置通知监听屏幕的旋转(在旋转的瞬间触发该方法)
首先我们需要先注册一个UIApplicationDidChangeStatusBarOrientationNotification
通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "statusBarOrientationChange:", name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil)
当触发屏幕旋转旋转事件的时候,就会执行我们的方法:
func statusBarOrientationChange(notification: NSNotification) {
let orientation = UIApplication.sharedApplication().statusBarOrientation
if (orientation == UIInterfaceOrientation.LandscapeRight) {
print("home键靠右")
}
if (orientation == UIInterfaceOrientation.LandscapeLeft) {
print("home键靠左")
}
if (orientation == UIInterfaceOrientation.Portrait) {
print("home键向下(默认情况)")
}
if (orientation == UIInterfaceOrientation.PortraitUpsideDown) {
print("home键向上")
}
}
如果想要在旋转完成后执行某些操作的话,需要重写如下方法:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
//你的处理逻辑
}