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

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

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

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

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

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

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

修改后的代码为:

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

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

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

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

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

下面以我弄的一个为例子,例子的名称为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>自动旋转和调整大小

想下载源代码的还去我的CSDN上面下载好了具体地址为:http://hanyegudeng.download.csdn.net/

欢迎指点!

原文地址:https://www.cnblogs.com/jackljf/p/3589031.html