iOS UIScreen詳解

盧三發表於2017-12-20

獲取主螢幕物件

UIScreen *screen =[UIScreen mainScreen];
複製程式碼

截圖

利用的當前在Screen上的部分生成一個UIView,利用這個UIVIew可以做一些全屏的動畫。注意,這樣的效果是比生成一副圖片的效率要高的。

- (UIView * nonnull)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
複製程式碼

效果如下:

20150719203348841.png

座標系

coordinateSpace 這個函式返回遵循UICoordinateSpace協議的物件,這個協議封裝了座標系的資訊以及座標系轉換的函式。UIView和UIScreen的物件都實現了這個協議。

 UIScreen * screen = [UIScreen mainScreen];
 id<UICoordinateSpace> coor = [screen coordinateSpace];
複製程式碼

把UIView的一個點進行座標系轉換到固定的UIScreen座標系

 [customView convertPoint:CGPointMake(30, 30) toCoordinateSpace:customView.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]];
}
複製程式碼

豎直方向

1.列印:0.000000 0.000000 320.000000 480.000000
2.列印: 0.000000 0.000000 640.000000 960.000000
複製程式碼

橫屏方向:

1.列印: 0.000000 0.000000 480.000000 320.000000
2.列印: 0.000000 0.000000 640.000000 960.000000
複製程式碼

NativeScale/Scale

從一個point到畫素點的轉換關係。例如:在普通螢幕上,1個點對應一個畫素,5s和6等視網膜螢幕1個點對應4個畫素。在6p上,一個點對應9個畫素。 亮度

通過修改屬性brightness來修改亮度。

[UIScreen mainScreen].brightness =0.9;
複製程式碼

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);
複製程式碼

結果為:

 Width:640.000000 Height:1136.000000 AspectRatio:1.000000
複製程式碼

相關文章