UIScreen详解:定时器,快照,亮度,尺寸等实现方法

UIScreen对象定义一些与硬件显示相关的属性。iOS设备有主屏幕或0到多个附加屏幕。

iOS8新增加了API定义竖屏和任意屏幕的CoordinateSpace方法,更加灵活。

获得屏幕

//获得主屏幕
let mainScreen = UIScreen.mainScreen()
//获得主屏幕及其附加的屏幕,一般情况下只有一个屏幕
let screens = UIScreen.screens()
坐标空间,iOS8新方法

//坐标空间,bounds随着屏幕方向变化而变化,如果横屏iPhone6为,(0.0, 0.0, 375.0, 667.0)
let coordinateSpace = mainScreen.coordinateSpace
//坐标空间,bounds始终时竖屏是的尺寸,iPhone6为(0.0,0.0,375.0,667.0)
let fixedCoordinateSpace = mainScreen.fixedCoordinateSpace

屏幕相关

//按点来计算,iPhone6为(0.0,0.0,375.0,667.0)
let bounds = mainScreen.bounds
//按点来算app window的frame,不包括状态条,iPhone6为(0.0,20.0,375.0,647.0)
let applicationFrame = mainScreen.applicationFrame
//按像素点来计算,物理屏幕的尺寸,iPhone6为(0.0,0.0,750.0,1334.0)
let nativeBounds = mainScreen.nativeBounds
//物理屏幕的比例因子,视网膜屏幕都是2.0
let nativeScale = mainScreen.nativeScale
//点和像素的比例,视网膜为2.0
let scale = mainScreen.scale
屏幕模式

let preferredModel = mainScreen.preferredMode.size
let preferredModelr = mainScreen.preferredMode.pixelAspectRatio

定时器

//定时器
let displayLink = mainScreen.displayLinkWithTarget(self, selector: Selector("refreshNumber"))
//120表示120帧(2秒)重复一次
displayLink.frameInterval  = 120
displayLink .addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
屏幕亮度

//调整屏幕的亮度,0-1之间的数字,亮度只在app为active状态有效,background状态会恢复
mainScreen.brightness = 0.5
//设置为YES时,会在屏幕上加一层dimming view,所以设置为0时也会有一定的亮度哦
mainScreen.wantsSoftwareDimming = true
屏幕快照

//屏幕快照:参数表示是否立即生产快照,如果立即生产快照则之后的一些变化不会被记录,返回一个UIView,能够对view添加内容,但是不能修改layer.contens。
mainScreen.snapshotViewAfterScreenUpdates(true)
UIScreen的相关通知
//有四个通知
UIScreenDidConnectNotification
UIScreenDidDisconnectNotification
UIScreenModeDidChangeNotification
UIScreenBrightnessDidChangeNotification

猜你喜欢

转载自blog.csdn.net/lcl130/article/details/42131325