iPhone之横竖屏与自动旋转
来源:互联网 发布:面瘫的治疗过程知乎 编辑:程序博客网 时间:2024/06/17 20:23
iPhone的自动旋转功能一共有三种方法:
使用自动调整属性处理旋转,利用系统自动生成的代码。
- (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+ ->(<-)即可。在旋转是重构视图(即手动设置每一个控件的位置和大小,让其重新排列)
第一种方法基本上不需要编写代码,这种方法就需要写点代码了,毕竟重新设置每一个控件的坐标了嘛。例如:
在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最终结果为:
- 切换视图
这种方法使用于比较复杂的界面,是需要分别设计横向模式和纵向模式,然后在使用的过程中自动切换。
首先定义输出口:(简单描述,设计两个视图,一个定义为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; }}
最后运行工程即可。
开始时候的图片:
旋转后并且按了Foo按钮后的图片:
- iPhone之横竖屏与自动旋转
- iphone横竖屏切换,旋转屏幕
- iOS之横竖屏旋转
- iOS之横竖屏旋转
- iOS的横竖屏切换旋转(禁自动旋转)
- cocos2d HelloWorld 项目横竖屏、自动旋转问题
- iphone 恒屏和自动旋转
- iphone自动旋转
- android 视频播放 点击横竖屏切换 自动旋转横竖屏切换 冲突
- iphone+横竖屏切换
- iphone横竖屏解决方案
- iPhone横竖屏转换
- iphone支持横竖屏
- android怎样实现关闭系统自动旋转屏幕时能够自动横竖屏
- iOS 横竖屏旋转处理
- h5 监听 横竖屏 旋转
- iphone 自动响应旋转屏幕
- iphone,ipad横竖屏切换
- Hibernate学习篇09——级联操作
- LeetCode 2. Add Two Numbers
- 机器学习基础-线性回归之梯度下降
- 分布式,集群与负载平衡是什么?
- Handler消息机制之基础代码篇
- iPhone之横竖屏与自动旋转
- .ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- PHP学习(八)--循环结构
- Hibernate学习篇10——自身一对多
- 解决myeclipse中中文字太小的问题
- KVC Deep Dip
- gdb multithread debugging
- iOS中的运行时浅析
- PHP学习(九)--特殊的流程控制语句