设备定向和旋转背后的秘密
在iOS中,设备的方向因用户的持有方式而异。 这些方向保存在枚举UIDeviceOrientation
。
UIDeviceOrientation是一个只读值。 尝试通过[[UIDevice currentDevice] orientation]
获取设备方向。 UIDeviceOrientation
在UIKit/UIDevice.h
定义。
UIInterfaceOrientation使用UIDeviceOrientation的值。 它专注于旋转。 UIInterfaceOrientation
在UIKit/UIApplication.h
定义。
UIInterfaceOrientation被描述为应用程序的方向。 因此,我们可以将应用程序强制设为纵向界面,甚至横向设备也是如此。 例如,无论您如何更改设备的方向,Facebook应用程序始终处于纵向界面方向。
设备如何判断方向?
通常,应用程序的方向在info.plist
中定义。
UISupportedInterfaceOrientations
UIInterfaceOrientationLandscapeRight
UIInterfaceOrientationLandscapeLeft
UISupportedInterfaceOrientations〜ipad
UIInterfaceOrientationLandscapeRight
UIInterfaceOrientationLandscapeLeft
好的,让我们首先回顾一下iOS应用的编译。 🤔
Clang编译器(前端)检查语法,然后生成中间表示形式,该中间表示形式是源和目标(应用)之间的中间程序。
LLVM编译器(后端)将所需的代码编译为BitCode(优化),然后根据体系结构(例如x86)将它们链接到不同的机器代码。
PS:Swift编译器(字体结尾)基于clang编译器。 Swift编译器将函数和属性转换为-Swift.h
然后clang将生成中间表示。
编译器在哪里处理Info.plist
? LLVM在目标中链接所需的框架后,它将编译xibs,imageAssets和info.plist等。
现在我们已经知道设备方向是在编译时决定的。 😁
UISupportedInterfaceOrientations
UIInterfaceOrientationLandscapeRight
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationPortrait