iphone 之 横屏和自动旋转

IPhone的自动旋转功能一共有3中方法:html

1.使用自动调整属性处理旋转。框架

利用系统自动生成的代码。ide

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);//系统默认不支持旋转功能
}ui

要想让系统自动实现旋转功能仅须要实现上面的代码,把return (interfaceOrientation == UIInterfaceOrientationPortrait)修改为为return OK便可。atom

修改后的代码为:url

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return OK;
}spa

而后在使用自动调整属性设计界面(Apple+3),指定要支持的方向便可。.net

在使用模拟仿真器的时候,要让其自动旋转只需Apple+ ->(<-)便可。设计

 

2.在旋转是重构视图。(也即手动设置每个控件的位置和大小,让其从新排列)orm

第一种方法基本上不须要编写代码,这种方法就须要写点代码了,毕竟从新设置每个控件的坐标了嘛。

下面以我弄的一个为例子,例子的名称为IP_05Autosize。

例如,首先看这个图片:

《Iphone开发基础教程》第五章 <wbr>自动旋转和调整大小

在View中添加6个button,而后设定W和H分别为125和125,这样在横向的时候这三个button是会重叠的,由于在横向的时候Iphone 支持的显示像素为480x300,没有状态栏的状况下是480x320.(在纵向时候显示像素为320x460,没有状态栏的状况下是320x480)。

如今就须要写代码从新排列这六个按钮了。

首先声明变量:

在IP_05AutosizeViewController.h中添加以下的代码:

#import <UIKit/UIKit.h>

@interface IP_05AutosizeViewController : UIViewController {
 IBOutlet UIButton *button1;
 IBOutlet UIButton *button2;
 IBOutlet UIButton *button3;
 IBOutlet UIButton *button4;
 IBOutlet UIButton *button5;
 IBOutlet UIButton *button6;
}
@property (nonatomic,retain)UIView *button1;
@property (nonatomic,retain)UIView *button2;
@property (nonatomic,retain)UIView *button3;
@property (nonatomic,retain)UIView *button4;
@property (nonatomic,retain)UIView *button5;
@property (nonatomic,retain)UIView *button6;

@end

而后在IP_05AutosizeViewController.m中,添加实现方法。

@implementation IP_05AutosizeViewController
@synthesize button1;
@synthesize button2;
@synthesize button3;
@synthesize button4;
@synthesize button5;
@synthesize button6;

-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
{
 UIInterfaceOrientation to=self.interfaceOrientation;
 if(to == UIInterfaceOrientationPortrait || to == UIInterfaceOrientationPortraitUpsideDown)
 {
  button1.frame = CGRectMake(20, 20, 125, 125);
  button2.frame = CGRectMake(175, 20, 125, 125);
  button3.frame = CGRectMake(20, 168, 125, 125);
  button4.frame = CGRectMake(175, 168, 125, 125);
  button5.frame = CGRectMake(20, 315, 125, 125);
  button6.frame = CGRectMake(175, 315, 125, 125);
 }
 else
 {
  button1.frame = CGRectMake(20, 20, 125, 125);
  button2.frame = CGRectMake(20, 155, 125, 125);
  button3.frame = CGRectMake(177, 20, 125, 125);
  button4.frame = CGRectMake(177, 155, 125, 125);
  button5.frame = CGRectMake(328, 20, 125, 125);
  button6.frame = CGRectMake(328, 155, 125, 125);
 }

}

还须要修改- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation的代码:

修改后为:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
   return (interfaceOrientation == UIInterfaceOrientationPortrait
   || interfaceOrientation == UIInterfaceOrientationLandscapeLeft
   || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

而后在dealloc中释放资源:

- (void)dealloc

{
 [button1 release];
 [button2 release];
 [button3 release];
 [button4 release];
 [button5 release];
 [button6 release];
 [super dealloc];
}

到此代码部分搞定,而后就是链接控制器和视图了。这点应该比较简单了。呵呵!

而后Build and Go最终结果为:

《Iphone开发基础教程》第五章 <wbr>自动旋转和调整大小

 

3.切换视图

这种方法使用于比较复杂的界面,是须要分别设计横向模式和纵向模式,而后在使用的过程当中自动切换。

固然了这个也须要肯定输出口和一些方法了。

首先定义输出口:

(简单描述,设计两个视图,一个定义为landscape,一个是portrait,一个为320x460,一个为480x300,每个输出口分别和每一个视图中的按钮想关联)

//用于切换的两个View

 IBOutlet UIView *landscape;
 IBOutlet UIView *portrait;
 //Foo两个View中的Foo按钮
 IBOutlet UIButton *landscapeFooButton;
 IBOutlet UIButton *portraitFooButton;
 //Bar两个View中的Bar按钮
 IBOutlet UIButton *landscapeBarButton;
 IBOutlet UIButton *portraitBarButton;

还须要File's Owner和两个View想关联。按住Ctrl将File's Owner拖到Portrait上面,在弹出灰色菜单上选择Portrait同理选择Landscape。而后在按住Ctrl将File's Owner拖到Landscape上面,在弹出的灰色菜单上选择View,让Landscape为自启动View。

而后是方法的实现:
-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)to duration:(NSTimeInterval)duration
{
 if(to == UIInterfaceOrientationPortrait)
 {
  self.view = self.portrait;
  self.view.transform = CGAffineTransformIdentity;
  self.view.transform = CGAffineTransformMakeRotation(degressToRadian(0));
  self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
 }
 else if (to == UIInterfaceOrientationLandscapeLeft)
 {
  self.view = self.landscape;
  self.view.transform = CGAffineTransformIdentity;
  self.view.transform = CGAffineTransformMakeRotation(degressToRadian(-90));
  self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
  
 }
 else if (to == UIInterfaceOrientationPortraitUpsideDown)
 {
  self.view = self.portrait;
  self.view.transform = CGAffineTransformIdentity;
  self.view.transform = CGAffineTransformMakeRotation(degressToRadian(180));
  self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
  
 }
 else if (to == UIInterfaceOrientationLandscapeRight)
 {
  self.view = self.landscape;
  self.view.transform = CGAffineTransformIdentity;
  self.view.transform = CGAffineTransformMakeRotation(degressToRadian(90));
  self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
  
 }

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

       return YES;
}

不要忘了在dealloc中释放资源哦。

由于在上面的代码中使用到了Core Graphics框架,所以要把该框架链接到该项目中,具体的方法是:在Resources上面点右键Add->Existing Frameworks。而后就是查找路径了。我第一次就看错了没有找到,哎,作事情不下心呀!具体路径为:/Developer/Platforms /iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System /Library/Frameworks/CoreGraphics.framework 呵呵,这个路径是有点长很差找,记住不是在:/Developer/SDKs里面了,我刚开始就是找到呢里面了。呵呵!还有就是导入后会处理一个提示框, 不要Copy,Reference Type要选择Relative to Current SDK而后add就OK了。

例子中还有一个方法是对View中的按钮实现隐藏的,这个就比较简单了!

具体方法为:

-(IBAction)buttonPressed:(id)sender
{
 if(sender == portraitFooButton||sender == landscapeFooButton)
 {
  portraitFooButton.hidden=YES;
  landscapeFooButton.hidden=YES;
 }
 else
 {
  portraitBarButton.hidden=YES;
  landscapeBarButton.hidden=YES;
 }

}

呵呵,上图两张看看:

刚开始运行时的图片

《Iphone开发基础教程》第五章 <wbr>自动旋转和调整大小

旋转后而且按了Foo按钮后的图片

《Iphone开发基础教程》第五章 <wbr>自动旋转和调整大小

相关文章
相关标签/搜索