iOS 横屏竖屏开发相关

来源:互联网 发布:商业银行 数据质量 编辑:程序博客网 时间:2024/06/15 14:20

引言:

iPhone的横屏竖屏针对iOS系统版本分为两种开发方式: 一种是iOS 6之前的使用模式 一种是iOS6的新模式. 两者的区别还是蛮大的.


参考:

1:IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)

http://blog.csdn.net/cococoolwhj/article/details/8208991



使用:

支持自动旋转?

iOS6之前通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法。如下示例,设置以后,屏幕被旋转时只支持横屏转换:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.    return UIInterfaceOrientationIsLandscape(interfaceOrientation);  
  4. }  

iOS6之后使用如下两个方法控制自动旋转,分别是:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (BOOL)shouldAutorotate  
  2. {  
  3.     NSLog(@"让不让我旋转?");  
  4.     return YES;  
  5. }  
  6.   
  7. - (NSUInteger)supportedInterfaceOrientations {  
  8.     NSLog(@"让我旋转哪些方向");  
  9.     return UIInterfaceOrientationMaskAllButUpsideDown;  
  10. }  



那么在自动旋转触发后,系统会自动调用另外两个方法:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {  
  2.     [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];  
  3.     NSLog(@"将要旋转了?");  
  4. }  
  5.   
  6. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {  
  7.     [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];  
  8.     NSLog(@"如果让我旋转,我已经旋转完了!");  
  9.       
  10. }  



2:让程序第一次启动时立刻显示横屏还是竖屏的方式

此处


3:传说中的私有API实现切换ViewController强制横屏的方式

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {  
  2.         [[UIDevice currentDevice] performSelector:@selector(setOrientation:)  
  3.                                        withObject:(id)UIInterfaceOrientationLandscapeRight];  
  4.     }  

4:使用xib进行界面设计时,改变xib的横竖显示方式

0 0
原创粉丝点击