iOS屏幕旋转的处理


设置通知监听屏幕的旋转(在旋转的瞬间触发该方法)

首先我们需要先注册一个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) {
        //你的处理逻辑         
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容