国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發中控制屏幕旋轉的方法

2020-02-19 15:50:29
字體:
來源:轉載
供稿:網友

隨著蘋果設備更新,設備的屏幕尺寸越來越大,設備上的應用程序可以自由旋轉,這也讓非常多果粉越來越關注,話不多說,接下來武林技術頻道一起來看看iOS開發中控制屏幕旋轉的方法吧!

在iOS5.1 和 之前的版本中, 我們通常利用 shouldAutorotateToInterfaceOrientation: 來單獨控制某個UIViewController的旋屏方向支持,比如:

?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?
{?
??? return (interfaceOrientation == UIInterfaceOrientationPortrait);?
}?

?

但是在iOS6中,這個方法被廢棄了,使用無效。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)

實踐后會發現,通過supportedInterfaceOrientations的單獨控制是無法鎖定屏幕的。

?

-(NSUInteger)supportedInterfaceOrientations?
{?
??? return UIInterfaceOrientationMaskPortrait;?
}?

?

多次實驗后總結出控制屏幕旋轉支持方向的方法如下:
子類化UINavigationController,增加方法

?

- (BOOL)shouldAutorotate?
{?
??? return self.topViewController.shouldAutorotate;?
}?
?
- (NSUInteger)supportedInterfaceOrientations?
{?
??? return self.topViewController.supportedInterfaceOrientations;?
}?


并且設定其為程序入口,或指定為 self.window.rootViewController
隨后添加自己的view controller,如果想禁止某個view controller的旋屏:(支持全部版本的控制)

?

?

?


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?
{?
??? return (interfaceOrientation == UIInterfaceOrientationPortrait);?
}?
?
-(BOOL)shouldAutorotate?
{?
??? return NO;?
}?
?
-(NSUInteger)supportedInterfaceOrientations?
{?
??? return UIInterfaceOrientationMaskPortrait;?
}?

?

如果想又開啟某個view controller的全部方向旋屏支持:

?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?
{?
??? return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);?
}?
?
-(NSUInteger)supportedInterfaceOrientations?
{?
??? return UIInterfaceOrientationMaskAllButUpsideDown;?
}?
?
-(BOOL)shouldAutorotate?
{?
??? return YES;?
}?

?

從而實現了對每個view controller的單獨控制。

順便提一下,如果整個應用所有view controller都不支持旋屏,那么干脆:

?

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window?
{?
???? return UIInterfaceOrientationMaskPortrait;?
}?


橫豎屏切換,視圖亂了怎么辦?
首先,我們必須了解一下下列4種狀態,它們被用來描述設備旋轉方向:

?

?

?

2015102495412717.png (408×178)

對于旋屏的處理,大致分為如下幾種情況和思路:
也許,你不需要旋屏支持,而希望鎖定屏幕

?

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?
{?
??? return NO;?
}?


也許,你需要支持旋屏,或者支持部分方向的旋屏

?

?

?


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {??
?????? return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);?
}?

?

也許,你的view有張背景圖,旋屏時系統幫助你拉伸了圖片,但是卻沒有管你的其它部件,比如button,你希望直接改變button的大小和位置

?

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration?
{?
??? if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {?
??????? NSLog(@"現在是豎屏");?
??????? [btn setFrame:CGRectMake(213, 442, 340, 46)];?
??? }?
??? if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {?
??????? NSLog(@"現在是橫屏");?
??????? [btn setFrame:CGRectMake(280, 322, 460, 35)];?
??? }?
}?


也許,你并不希望用絕對坐標去約束控件,而是希望讓它通過旋轉自己適應屏幕的旋轉

?

?

?


- (void)viewDidLoad?
{?
??? [super viewDidLoad];?
??? // Do any additional setup after loading the view, typically from a nib.?
??? UIDevice *device = [UIDevice currentDevice];??
??? [device beginGeneratingDeviceOrientationNotifications];?
??? //利用 NSNotificationCenter 獲得旋轉信號 UIDeviceOrientationDidChangeNotification?
??? NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];??
??? [ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device];?
}?
?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?
{?
??? return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);?
}?
?
-(void)rotation_btn:(float)n?
{?
??? UIButton *robtn = self.btn;?
??? robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);?
}?
?
-(void)orientationChanged?
{?
??? UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];?
?????
??? switch (orientaiton) {?
??????? caseUIDeviceOrientationPortrait:??????????????
??????????? [self? rotation_btn:0.0];?
??????????? break;?
??????? caseUIDeviceOrientationPortraitUpsideDown:???
??????????? [self? rotation_btn:90.0*2];?
??????????? break;?
??????? caseUIDeviceOrientationLandscapeLeft:??????
??????????? [self? rotation_btn:90.0*3];?
??????????? break;?
??????? caseUIDeviceOrientationLandscapeRight:???
??????????? [self? rotation_btn:90.0];?
??????????? break;?
??????? default:?
??????????? break;?
??? }?
}?


也許,你需要autoresizesSubviews = YES
也許,你希望橫豎屏有不同的布局效果,需要準備2份Subview,在不同狀態去替換
當然不要忘記,需要調節設定圖示中的1、2處,

?

?

?

2015102495438507.png (258×296)

來幫助我們完成自己想要的適應效果。Example 動畫呈現的很清晰,^_^ 我就不再啰嗦了。

以上就是武林技術頻道小編為各位朋友們總結的iOS開發中控制屏幕旋轉的方法,希望可以幫助到大家。還有什么問題的話,大家可以關注武林技術頻道。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 瓦房店市| 成安县| 彰化市| 临夏市| 宁化县| 萨迦县| 乌鲁木齐县| 镇康县| 海阳市| 保靖县| 芦溪县| 永丰县| 行唐县| 清涧县| 固始县| 鄂托克前旗| 西城区| 新龙县| 衡山县| 桐庐县| 平昌县| 阿坝| 长沙市| 义乌市| 拉孜县| 平果县| 霞浦县| 三江| 沙雅县| 赣州市| 深水埗区| 高碑店市| 德庆县| 沙雅县| 霍城县| 乐清市| 黄冈市| 黔东| 武城县| 麻栗坡县| 易门县|