iOS屏幕旋轉(zhuǎn)
1.基本屬性和概念
- shouldAutorotate </br>
Returns a Boolean value indicating whether the view controller'??s contents should auto rotate.
控制器的內(nèi)容是否自動(dòng)旋轉(zhuǎn)
- supportedInterfaceOrientations </br>
Returns all of the interface orientations that the view controller supports.
控制器支持的選裝方向
注意:
UIDeviceOrientation 設(shè)備方向
UIInterfaceOrientation 屏幕視圖方向
豎屏?xí)r設(shè)備方向和屏幕方向是一致的
橫屏?xí)r設(shè)備方向和屏幕方向相反,如手機(jī)右轉(zhuǎn)(home鍵在右側(cè))時(shí),屏幕方向是左轉(zhuǎn)的揣苏。
有部分三方?jīng)]有及時(shí)升級(jí) 屏幕旋轉(zhuǎn)后 View顯示反了
- preferredInterfaceOrientationForPresentation
Returns the interface orientation to use when presenting the view controller.
present View Controller 優(yōu)先顯示的方向
根控制器控制視圖的是否支持自動(dòng)旋轉(zhuǎn)和旋轉(zhuǎn)方向
即根控制器是UINavigationController
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
2采郎、場(chǎng)景: 項(xiàng)目一直豎屏,現(xiàn)在有一個(gè)界面想橫屏
第一種Push的方式
第一步:Device Orientaion中勾選Landscape Left 和 Landscape Right
第二步:基類(lèi)VC自動(dòng)旋轉(zhuǎn)和支持方向的方法
//根視圖默認(rèn)不支持自動(dòng)旋轉(zhuǎn)
- (BOOL)shouldAutorotate
{
return NO;
}
// 支持豎屏顯示方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
第三步:在目標(biāo)VC界面
//目標(biāo)視圖支持自動(dòng)旋轉(zhuǎn)
- (BOOL)shouldAutorotate
{
return YES;
}
// 支持豎屏顯示方向 -> UIInterfaceOrientationMaskLandscape
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
第四步:目標(biāo)VC界面添加
//強(qiáng)制轉(zhuǎn)屏
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
//轉(zhuǎn)屏后回調(diào)
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0)
{
// NSLog(@"%d",[NSThread isMainThread]);
// NSLog(@"%@",NSStringFromCGSize(size));
// 記錄當(dāng)前是橫屏還是豎屏
// 翻轉(zhuǎn)的時(shí)間
CGFloat duration = [coordinator transitionDuration];
[UIView animateWithDuration:duration animations:^{
//轉(zhuǎn)屏后刷新UI坐標(biāo)
[self reloadLandscapeView:size];
}];
}
第二種present的方式
第一步:同上
第二步:同上 基類(lèi)VC多添加一個(gè)方法
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
第三步:同上 目標(biāo)VC多添加一個(gè)方法
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
第四步:目標(biāo)VC界面重寫(xiě)方法
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
{
[super willTransitionToTraitCollection:newCollection
withTransitionCoordinator:coordinator];
[self reloadLandscapeView:CGSizeMake(MAX(self.view.frame.size.width, self.view.frame.size.height), MIN(self.view.frame.size.width, self.view.frame.size.height))];
}