如果你需要開啟旋轉
首先你需要開啟旋轉在info.plist點選(需要使用方向)
- Portrait
- Landscape Left
- Landscape Right
或者在AppDelegate中實現代理方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortraitUpsideDown;
}
系統提供的樣式:無非是各種搭配一下和plist里的一樣
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
}
- 首先讓我們來看看系統在程序的啟動過程UIKit處理屏幕旋轉的流程
當加速計檢測到方向變化的時候,會發出 UIDeviceOrientationDidChangeNotification 通知,這樣任何關心方向變化的view都可以通過注冊該通知,在設備方向變化的時候做出相應的響應。UIKit幫助我們做了很多事情,方便我們完成屏幕旋轉。UIKit的相應屏幕旋轉的流程如下:
1、設備旋轉的時候,UIKit接收到旋轉事件。
2、UIKit通過AppDelegate通知當前程序的window。
3、Window會知會它的rootViewController,判斷該view controller所支持的旋轉方向,完成旋轉。
4、如果存在彈出的view controller的話,系統則會根據彈出的view controller,來判斷是否要進行旋轉。
看了這么久你會發現,道理我都懂但是咋使用呢?
****NAV PushViewController 單獨設置某頁****
注 : 使用UINavigationController 父類實現不然子VC無效(原因就是在有UINavigationController的情況下rootViewController是UINavigationController)
父類實現方法,就可以讓某個單獨的VC獲得效果:
- (BOOL)shouldAutorotate
{
//也可以用topViewController判斷VC是否需要旋轉
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
//也可以用topViewController判斷VC支持的方向
return self.topViewController.supportedInterfaceOrientations;
}
****Tabbat PushViewController 單獨設置某頁****
- (BOOL)shouldAutorotate {
return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
return [self.selectedViewController supportedInterfaceOrientations];
}
tabbr+nav 需要同時設置才能生效
子類實現方法
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//當前支持的旋轉類型
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate
{
// 是否支持旋轉
return YES;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// 默認進去類型
return UIInterfaceOrientationPortrait;
}
PresentViewController 單獨設置某頁
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
// 是否支持旋轉
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
// 是否支持旋轉
return YES;
}
iOS屏幕旋轉各類集錦(二)-單頁部分旋轉
***寫的比較粗糙demo附上https://github.com/bloodspasm/ScreenRotation ***