UIScreen 和UIWindow
UIScreen object defines the properties associated with a hardware-based display
就是说UIScreen是硬件显示器的软件表示。通过UIScreen来获取和设置对应显示器的属性。一个设备可以有一个主屏幕和若干附属屏幕(attached screens)。
A UIWindow object provides the backdrop for your app’s user interface and provides important event-handling behaviors
UIWindow是用户界面的载体,用户的输入首先被对应的UIWindow接收,然后被转发到其他的view上。
UIScreen和UIWindow的关系
UIWindow
rootViewController: UIViewControllerscreen: UIScreenkeywindow
A window is considered the key window when it is currently receiving keyboard and non touch-related events.
就是说如果一个window可以接收键盘和非触摸事件,那么这个就叫做keywindow。
事件响应流是这样的。
isKeyWindow: Bool { get }如何把一个window变成keywindow
调用makeKeyAndVisible()方法
调用这个方法会把这个window变成keywindow,并在同级别的level中在最前面展示。因此,keywindow不一定显示在最前面,可能在z轴方向上有一个window在keywindow上面。
若想把一个window变成可见的,只需要设置hidden属性为no就好了。
// Show the window, but do not make it key
self.externalWindow!.hidden = false
调用makeKey()方法。
这个方法不改变window的可见性。
keywindow的生命周期
UIWindowDidBecomeKey通知,调用becomeKey()方法UIWindowDidBecomeVisible方法。UIWindowDidBecomeHidden方法放弃成为keywindow
发送UIWindowDidResignKey通知,调用resignKey()方法
UIScreen
class func main() -> UIScreenclass func screens() -> [UIScreen]。mainScreen永远是第一个。获取屏幕分辨率
var bounds: CGRect { get }。var nativeBounds: CGRect { get }。UIScreen.mainScreen().fixedCoordinateSpace.boundsUIScreen.mainScreen().coordinateSpace.boundsnativeScale和scale
无论是CoreGraphics还是图片scale选取,都应该以[UIScreen scale]为准。
设置屏幕的亮度
var brightness: CGFloat { get set }。范围是0到1,1最亮。下次锁屏之前一直有效。如果锁屏然后解锁,恢复到系统亮度。
屏幕刷新时的回调
let ca1 = UIScreen.mainScreen().displayLinkWithTarget(self, selector: #selector(ViewController.testCADisplay))
ca1?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
相关通知
相关参考资料
原文:http://www.cnblogs.com/huahuahu/p/UIScreen-heUIWindow.html