您的位置:首页 > 产品设计 > UI/UE

iOS SDK详解之UIScreen

2015-07-19 16:04 573 查看
原创Blog,转载请注明出处

blog.csdn.net/hello_hwc

欢迎关注我的iOS SDK详解专栏

http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html

不知不觉这个专栏已经写了45篇文章了,我想应该至少还有100篇吧。iOS开发的东西太多太多

前言:UIScreen封装访问着硬件显示屏相关的信息。一个iOS设备通畅只有一个硬件显示屏。当然,如果通过USB或者无线投影到其他显示器的时候,就有可能有多个。


对于链接多个屏幕的情况,本文并不会讲解,因为绝大多数App用不到这个功能。



获取主屏幕对象

UIScreen * screen = [UIScreen mainScreen];


截屏

利用的当前在Screen上的部分生成一个UIView,利用这个UIVIew可以做一些全屏的动画。注意,这样的效果是比生成一副图片的效率要高的。

- (UIView * nonnull)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates


例如

self.view.backgroundColor = [UIColor redColor];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIScreen * mainScreen  = [UIScreen mainScreen];
        UIView * snapshot = [mainScreen snapshotViewAfterScreenUpdates:NO];
        snapshot.frame = CGRectMake(0, 0,mainScreen.bounds.size.width/2,mainScreen.bounds.size.height/2);
        snapshot.center = self.view.center;
        [self.view addSubview:snapshot];
        self.view.backgroundColor = [UIColor whiteColor];

    });


效果



坐标系相关

coordinateSpace


这个函数返回遵循
UICoordinateSpace
协议的对象,这个协议封装了坐标系的信息以及坐标系转换的函数。UIView和UIScreen的对象都实现了这个协议。

UIScreen * screen = [UIScreen mainScreen];
    id<UICoordinateSpace> coor = [screen coordinateSpace];
    [self LogFrame:[coor bounds]];


把UIView的一个点进行坐标系转换到固定的UIScreen坐标系

[myView convertPoint:point toCoordinateSpace:myView.window.screen.fixedCoordinateSpace];


fixedCoordinateSpace



和上文CoordinateSpace唯一不同的是,这个返回的是 portrait-up的坐标系,也就是说是不变的。而CoordinateSpace会随着设备下旋转而改变。



Bounds/NativeBounds

bounds 以点为单位,随着屏幕方向变化而变化

NativeBounds 以像素为单位,固定为portrait-up的坐标系

举例

监听屏幕旋转并且log

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    UIScreen * screen = [UIScreen mainScreen];
    [self LogFrame:[screen bounds]];
    [self LogFrame:[screen nativeBounds]];
}




2015-07-19 18:02:27.417 NewOcTest[2615:147772] 0.000000 0.000000 320.000000 480.000000
2015-07-19 18:02:27.417 NewOcTest[2615:147772] 0.000000 0.000000 640.000000 960.000000




2015-07-19 18:02:23.455 NewOcTest[2615:147772] 0.000000 0.000000 480.000000 320.000000
2015-07-19 18:02:23.456 NewOcTest[2615:147772] 0.000000 0.000000 640.000000 960.000000


NativeScale/Scale

从一个point到像素点的转换关系。例如:在普通屏幕上,1个点对应一个像素,5s和6等视网膜显示屏1个点对应4个像素。在6p上,一个点对应9个像素。

普通屏幕上

NativeScale//1
Scale //1


5s 6

NativeScale//2
Scale //2


6p

NativeScale//3
Scale //3


亮度

通过修改属性
brightness
来修改亮度。

Screen Mode

三个属性

preferredMode 偏好的显示模式

availableModes 支持的显示模式

currentMode 当前的显示模式

返回对象是
UIScreenMode
,包含了

size 屏幕的大小,以像素为单位

pixelAspectRatio,像素的横纵比

举例

UIScreen * screen = [UIScreen mainScreen];
    UIScreenMode  *perferedMode = screen.preferredMode;
    NSLog(@"Width:%f Height:%f AspectRatio:%f",perferedMode.size.width,perferedMode.size.height,perferedMode.pixelAspectRatio);


5s输出

2015-07-19 20:42:00.791 NewOcTest[860:31850] Width:640.000000 Height:1136.000000 AspectRatio:1.000000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: