IOS重力感应

Phone和iPad设备有4个方向的状态,我们可以针对应用当前所处的方向调整界面。

为了使应用支持不同的方向,首先我们需要在项目设置中设置设备支持的方向(也可以在项目的plist中设置)

Portrait  竖放,home键在屏幕下方

Upside Down  竖放,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

用户界面旋转过程中触发此函数,一般在此函数中定制翻转后控件的位置和大小

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容