iOS屏幕旋转各类集锦(一)-单页旋转

如果你需要开启旋转
首先你需要开启旋转在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 ***

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,300评论 4 61
  • iOS屏幕旋转学习笔记iOS开发中使用屏幕旋转功能的相关方法 1、基本知识点解读 了解屏幕旋转首先需要区分两种 o...
    Laughingg阅读 13,611评论 13 39
  • 更好的阅读体验,请到个人博客阅读: iOS中的系统转场 请忽略标题,😂,本文记录的是对下图所示的Kind, Pre...
    CaryaLiu阅读 2,443评论 0 1
  • 三月有你 四月有你 五月你走了 六月是他自己 七月八月是雨季 九月十月连泡沫里带着香气 冬月煮酒腊月烧起了肉 一月...
    Fanderix阅读 195评论 0 0
  • 蓝色的碎片,红色的碎片,纸上的马赛克交织成了一个梦。 红色的身体,并不代表着高傲;蓝色的天空,承载起优雅的姿态。兔...
    漫慢书阅读 245评论 0 3