您好,登錄后才能下訂單哦!
處理IPhone屏幕的旋轉是我們經常遇到的,當你做一個應用既然滿足豎屏又要滿足橫屏,這就要求我們會處理屏幕旋轉的問題!
方法一:自動布局
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);} }
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); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。