转载自:http://blog.csdn.net/hello_hwc/article/details/46955529
原创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用不到这个功能。
获取主屏幕对象
<code class="hljs fix has-numbering"><span class="hljs-attribute">UIScreen * screen </span>=<span class="hljs-string"> [UIScreen mainScreen];</span></code><ul class="pre-numbering" style=""><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>
截屏
利用的当前在Screen上的部分生成一个UIView,利用这个UIVIew可以做一些全屏的动画。注意,这样的效果是比生成一副图片的效率要高的。
<code class="hljs erlang has-numbering"><span class="hljs-pp">- <span class="hljs-params">(<span class="hljs-variable">UIView</span> * nonnull)</span>snapshotViewAfterScreenUpdates:<span class="hljs-params">(<span class="hljs-variable">BOOL</span>)</span>afterUpdates </span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
例如
<code class="hljs avrasm has-numbering"> self<span class="hljs-preprocessor">.view</span><span class="hljs-preprocessor">.backgroundColor</span> = [UIColor redColor]<span class="hljs-comment">;</span> dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<span class="hljs-number">2</span> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UIScreen * mainScreen = [UIScreen mainScreen]<span class="hljs-comment">;</span> UIView * snapshot = [mainScreen snapshotViewAfterScreenUpdates:NO]<span class="hljs-comment">;</span> snapshot<span class="hljs-preprocessor">.frame</span> = CGRectMake(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>,mainScreen<span class="hljs-preprocessor">.bounds</span><span class="hljs-preprocessor">.size</span><span class="hljs-preprocessor">.width</span>/<span class="hljs-number">2</span>,mainScreen<span class="hljs-preprocessor">.bounds</span><span class="hljs-preprocessor">.size</span><span class="hljs-preprocessor">.height</span>/<span class="hljs-number">2</span>)<span class="hljs-comment">;</span> snapshot<span class="hljs-preprocessor">.center</span> = self<span class="hljs-preprocessor">.view</span><span class="hljs-preprocessor">.center</span><span class="hljs-comment">;</span> [self<span class="hljs-preprocessor">.view</span> addSubview:snapshot]<span class="hljs-comment">;</span> self<span class="hljs-preprocessor">.view</span><span class="hljs-preprocessor">.backgroundColor</span> = [UIColor whiteColor]<span class="hljs-comment">;</span> })<span class="hljs-comment">;</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul>
效果
坐标系相关
coordinateSpace
这个函数返回遵循UICoordinateSpace
协议的对象,这个协议封装了坐标系的信息以及坐标系转换的函数。UIView和UIScreen的对象都实现了这个协议。
<code class="hljs objectivec has-numbering"> UIScreen * screen = [UIScreen mainScreen]; <span class="hljs-keyword">id</span><UICoordinateSpace> coor = [screen coordinateSpace]; [<span class="hljs-keyword">self</span> LogFrame:[coor bounds]];</code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li></ul>
把UIView的一个点进行坐标系转换到固定的UIScreen坐标系
<code class="hljs avrasm has-numbering">[myView convertPoint:point toCoordinateSpace:myView<span class="hljs-preprocessor">.window</span><span class="hljs-preprocessor">.screen</span><span class="hljs-preprocessor">.fixedCoordinateSpace</span>]<span class="hljs-comment">;</span></code><ul class="pre-numbering" style=""><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>
fixedCoordinateSpace
和上文CoordinateSpace唯一不同的是,这个返回的是 portrait-up的坐标系,也就是说是不变的。而CoordinateSpace会随着设备下旋转而改变。
Bounds/NativeBounds
- bounds 以点为单位,随着屏幕方向变化而变化
- NativeBounds 以像素为单位,固定为portrait-up的坐标系
举例
监听屏幕旋转并且log
<code class="hljs objectivec has-numbering">-(<span class="hljs-keyword">void</span>)didRotateFromInterfaceOrientation:(<span class="hljs-built_in">UIInterfaceOrientation</span>)fromInterfaceOrientation{ UIScreen * screen = [UIScreen mainScreen]; [<span class="hljs-keyword">self</span> LogFrame:[screen bounds]]; [<span class="hljs-keyword">self</span> LogFrame:[screen nativeBounds]]; }</code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
竖
<code class="hljs css has-numbering">2015<span class="hljs-tag">-07-19</span> 18<span class="hljs-pseudo">:02</span><span class="hljs-pseudo">:27</span><span class="hljs-class">.417</span> <span class="hljs-tag">NewOcTest</span><span class="hljs-attr_selector">[2615:147772]</span> 0<span class="hljs-class">.000000</span> 0<span class="hljs-class">.000000</span> 320<span class="hljs-class">.000000</span> 480<span class="hljs-class">.000000</span> 2015<span class="hljs-tag">-07-19</span> 18<span class="hljs-pseudo">:02</span><span class="hljs-pseudo">:27</span><span class="hljs-class">.417</span> <span class="hljs-tag">NewOcTest</span><span class="hljs-attr_selector">[2615:147772]</span> 0<span class="hljs-class">.000000</span> 0<span class="hljs-class">.000000</span> 640<span class="hljs-class">.000000</span> 960<span class="hljs-class">.000000</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
横
<code class="hljs css has-numbering">2015<span class="hljs-tag">-07-19</span> 18<span class="hljs-pseudo">:02</span><span class="hljs-pseudo">:23</span><span class="hljs-class">.455</span> <span class="hljs-tag">NewOcTest</span><span class="hljs-attr_selector">[2615:147772]</span> 0<span class="hljs-class">.000000</span> 0<span class="hljs-class">.000000</span> 480<span class="hljs-class">.000000</span> 320<span class="hljs-class">.000000</span> 2015<span class="hljs-tag">-07-19</span> 18<span class="hljs-pseudo">:02</span><span class="hljs-pseudo">:23</span><span class="hljs-class">.456</span> <span class="hljs-tag">NewOcTest</span><span class="hljs-attr_selector">[2615:147772]</span> 0<span class="hljs-class">.000000</span> 0<span class="hljs-class">.000000</span> 640<span class="hljs-class">.000000</span> 960<span class="hljs-class">.000000</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
NativeScale/Scale
从一个point到像素点的转换关系。例如:在普通屏幕上,1个点对应一个像素,5s和6等视网膜显示屏1个点对应4个像素。在6p上,一个点对应9个像素。
普通屏幕上
<code class="hljs cs has-numbering">NativeScale<span class="hljs-comment">//1</span> Scale <span class="hljs-comment">//1</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
5s 6
<code class="hljs cs has-numbering">NativeScale<span class="hljs-comment">//2</span> Scale <span class="hljs-comment">//2</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
6p
<code class="hljs cs has-numbering">NativeScale<span class="hljs-comment">//3</span> Scale <span class="hljs-comment">//3</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
亮度
通过修改属性brightness
来修改亮度。
Screen Mode
三个属性
- preferredMode 偏好的显示模式
- availableModes 支持的显示模式
- currentMode 当前的显示模式
返回对象是UIScreenMode
,包含了
- size 屏幕的大小,以像素为单位
- pixelAspectRatio,像素的横纵比
举例
<code class="hljs avrasm has-numbering"> UIScreen * screen = [UIScreen mainScreen]<span class="hljs-comment">;</span> UIScreenMode *perferedMode = screen<span class="hljs-preprocessor">.preferredMode</span><span class="hljs-comment">;</span> NSLog(@<span class="hljs-string">"Width:%f Height:%f AspectRatio:%f"</span>,perferedMode<span class="hljs-preprocessor">.size</span><span class="hljs-preprocessor">.width</span>,perferedMode<span class="hljs-preprocessor">.size</span><span class="hljs-preprocessor">.height</span>,perferedMode<span class="hljs-preprocessor">.pixelAspectRatio</span>)<span class="hljs-comment">;</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li></ul>
5s输出
<code class="hljs css has-numbering">2015<span class="hljs-tag">-07-19</span> 20<span class="hljs-pseudo">:42</span><span class="hljs-pseudo">:00</span><span class="hljs-class">.791</span> <span class="hljs-tag">NewOcTest</span><span class="hljs-attr_selector">[860:31850]</span> <span class="hljs-tag">Width</span><span class="hljs-pseudo">:640</span><span class="hljs-class">.000000</span> <span class="hljs-tag">Height</span><span class="hljs-pseudo">:1136</span><span class="hljs-class">.000000</span> <span class="hljs-tag">AspectRatio</span><span class="hljs-pseudo">:1</span><span class="hljs-class">.000000</span> </code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>