
Phone和iPad设备有4个方向的状况,我们可以针对应用目前所处的方向调整页面。
为了使使用支持不同的方向,首先我们需要在项目设置中增设设备支持的方向(也可以在项目的plist中增设)

Portrait竖放,home键在萤幕下方
Upside Down竖放ios重力感应失效解决,home键在萤幕上方
Landscape Left横放,home键在萤幕左方

Landscape Right横放,home键在萤幕右方
显然横屏状态下如此的显示是不适于的,为了得到恰当的显示作用,我们需要在ViewController中写一些源码。
首先我们先看一下在ViewController中和重力感应相关的一些索引
- (BOOL)shouldAutorotate
此字段返回YES表示此字段控制器支持重力感应,返回NO表示此字段控制器不支持重力感应
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

此字段设置字段控制器加载后最先显示的方向,UIInterfaceOrientation是一个结构体,支持的取值如下
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
- (NSUInteger)supportedInterfaceOrientations

此字段设置字段控制器支持的方向(需要shouldAutorotate返回YES),支持的取值如下
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape

UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait| UIInterfaceOrientationMaskLandscapeLeft| UIInterfaceOrientationMaskLandscapeRight| UIInterfaceOrientationMaskPortraitUpsideDown)
UIInterfaceOrientationMaskAllButUpsideDown
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
用户界面即将旋转时触发此字段,toInterfaceOrientation表示刚刚抵达的方向
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
用户界面旋转结束时触发此字段,fromInterfaceOrientation表示旋转前的方向
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
用户界面旋转过程中触发此字段ios重力感应失效解决,一般在此函数中定做翻转后控件的位置和形状
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/sanxing/article-119375-1.html
但是好费电