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

来源:互联网 发布:并查集的数组 编辑:程序博客网 时间:2024/05/06 22:13

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

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

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