iPhone自动旋转控制代码-IOS开发

来源:互联网 发布:mac滴管粉底液 编辑:程序博客网 时间:2024/05/21 08:40

旋转有时候是很好的特性,但是并不是所有的程序界面都想旋转的,因为旋转会使得界面变得不和谐,除非你已经开发了专门针对各种方向的界面,所以有时候还是禁用旋转比较好,或者程序中的某个界面是横屏的,退出这个横屏视图之后界面又变成竖屏的,比如看视频或者浏览网页的时候你希望是横屏的,但是其他的工作你希望是竖屏的。OK,这一切都不是问题。我们可以通过代码来控制我们程序中每个界面的旋转功能。

在你想要设置的视图控制器里找到 shouldAutorotateToInterfaceOrientation: 方法,重写它的实现代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);}
系统通过调用此方法询问试图控制是否旋转到指定方向。系统共定义了4种方向,分别对应4种常见握持方式:
typedef enum {    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft} UIInterfaceOrientation;
如果想要对任何情况都支持旋转只要返回YES即可,如果只想部分支持就对支持的返回YES 不支持的返回NO。


最后附上Demo代码:AutoOreintationDemo


文章出处:http://m.blog.csdn.net/blog/iukey/7331757

0 0