关于IOS屏幕的旋转问题-创新互联

处理IPhone屏幕的旋转是我们经常遇到的,当你做一个应用既然满足竖屏又要满足横屏,这就要求我们会处理屏幕旋转的问题!

成都创新互联是一家专注于做网站、网站制作与策划设计,三河网站建设哪家好?成都创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:三河等地区。三河做网站价格咨询:13518219792

方法一:自动布局

1.将项目中界面的四种手持方式都点上;

2.取消Use Autolayout;

3.选择界面中某个控件然后到属性工具栏中去找到AutoSizing功能,勾选对应的绝对定位的线条

4.重写可以旋转的方法

-(BOOL)shouldAutorotate {     return YES; } -(NSUInteger)supportedInterfaceOrientations {     return UIInterfaceOrientationMaskAll; }


方法二:手动布局一(通过代码改view种控件的坐标)

1.重写可以旋转的方法

-(BOOL)shouldAutorotate {     return YES; } -(NSUInteger)supportedInterfaceOrientations {     return UIInterfaceOrientationMaskAll; }
2.勾选上项目中支持的四种手持类型

3.取消Use Autolayout

4.代码实现:

//每当屏幕旋转的时候都会触发一个 -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {     //如果是是横屏状态     if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft )     {         self.l1.frame = CGRectMake(20, 25, 110, 110);         self.l2.frame = CGRectMake(162, 25, 110, 110);         self.l3.frame = CGRectMake(304, 25, 110, 110);         self.r1.frame = CGRectMake(20, 178, 110, 110);         self.r2.frame = CGRectMake(162, 178, 110, 110);         self.r3.frame = CGRectMake(304, 178, 110, 110);} }

方法三:手动布局二(在xib中新建一个支持横屏的view通过双view切换实现)

1.在xib文件中拖一个view控件,选择Orientation属性为横屏

2.布局好界面

3.将横纵view分别在controller.h文件中创建对应的属性,命名为

@property (retain, nonatomic) IBOutlet UIView *landspaceView;

@property (retain, nonatomic) IBOutlet UIView *portatiorView;

4.代码实现

宏定义实现角度转弧度

#define degreesToRadia(x) (M_PI * (x) / 180)//参数要加括号 ,尤其是参数附近特别要加括号

-(BOOL)shouldAutorotate {     return YES; } -(NSUInteger)supportedInterfaceOrientations {     return UIInterfaceOrientationMaskAll; } //每当屏幕旋转的时候都会触发一个 -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {     //如果是是横屏状态     if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft )     { //        self.l1.frame = CGRectMake(20, 25, 110, 110); //        self.l2.frame = CGRectMake(162, 25, 110, 110); //        self.l3.frame = CGRectMake(304, 25, 110, 110); //        self.r1.frame = CGRectMake(20, 178, 110, 110); //        self.r2.frame = CGRectMake(162, 178, 110, 110); //        self.r3.frame = CGRectMake(304, 178, 110, 110);         self.view = self.landspaceView;         //self.view.transform = CGAffineTransformIdentity;         self.view.transform = CGAffineTransformMakeRotation(degreesToRadia(270));         self.view.bounds = CGRectMake(0, 0, 480, 300);     }     else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)     { //        self.l1.frame = CGRectMake(37, 20, 110, 110); //        self.l2.frame = CGRectMake(37, 162, 110, 110); //        self.l3.frame = CGRectMake(37, 304, 110, 110); //        self.r1.frame = CGRectMake(190, 20, 110, 110); //        self.r2.frame = CGRectMake(190, 162, 110, 110); //        self.r3.frame = CGRectMake(190, 304, 110, 110);         self.view = self.landspaceView;         //self.view.transform = CGAffineTransformIdentity;         self.view.transform = CGAffineTransformMakeRotation(degreesToRadia(90));         self.view.bounds = CGRectMake(0, 0, 480, 300);     }     else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)     {         self.view = self.portatiorView;         self.view.transform = CGAffineTransformIdentity;         self.view.bounds = CGRectMake(0, 0, 320, 460);     }     else if(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)     {         self.view = self.portatiorView;         //self.view = self.landspaceView;         //self.view.transform = CGAffineTransformIdentity;         self.view.transform = CGAffineTransformMakeRotation(degreesToRadia(180));         self.view.bounds = CGRectMake(0, 0, 320, 460);     } }

模拟屏幕旋转
commond + 方向键

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:关于IOS屏幕的旋转问题-创新互联
当前地址:http://pwwzsj.com/article/dhpici.html