UI篇-关于单个页面屏幕旋转要注意的问题

前言

有时候,我们会需要在整个项目中,使某一个ViewController支持屏幕旋转,而其他的ViewController并不能自动旋转。这是一个很常见的需求,下面就屏幕旋转相关问题做个小结。


强制页面旋转(假的屏幕旋转)

最多见的是,视屏播放中的横屏模式,点击全屏按钮,播放页面横屏最大化。使用
CGAffineTransformMakeRotation旋转操作配合动画即可。

-(void)fullScreenClick: (UIButton *)btn {
    btn.selected = !btn.selected;   
    if (btn.selected) {
    self.mySuperView = self.superview;
    [UIView animateWithDuration:0.3 animations:^{
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        self.transform = CGAffineTransformMakeRotation(M_PI / 2);
    } completion:nil];
    self.frame = self.bigFrame;

}else {
    [self removeFromSuperview];
    [self.mySuperView addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeRotation(M_PI * 2);
    } completion:nil];
    self.frame = self.smallFrame;
  }  
}

强制带有导航条的整个页面旋转

- (void)changeScreent :(BOOL)toRight
{
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView animateWithDuration:duration animations:^{
        // 修改状态栏的方向及view的方向进而强制旋转屏幕
        [[UIApplication sharedApplication] setStatusBarHidden:toRight];
        self.navigationController.view.transform = toRight?CGAffineTransformMakeRotation(M_PI_2): CGAffineTransformIdentity;
        if (toRight) {
            self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x, self.navigationController.view.bounds.origin.y, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
        }else{
            self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x, self.navigationController.view.bounds.origin.y, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        }   
    }];
    
}

注意状态栏无法强制旋转,在手机方向不变的情况下,所以,最好把状态栏隐藏掉,回复的时候再显示出来。不可使用self.view.frame.size.width self.view.frame.size.height,这样会出现第一次旋转出现上下部分白边的Bug,需要使用 [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height。

自动屏幕旋转

系统支持横屏的顺序

系统支持横屏顺序为以下几种,前面的会使后面的方法失效,优先级依次降低。

  • 默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选
Paste_Image.png
  • application window设置的级别次之

    application支持所有
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;//支持所有方向
    }
    
  • UINavigationcontroller里设置

     - (BOOL)shouldAutorotate//是否支持旋转屏幕{
      return YES;
    }
    - (NSUInteger)supportedInterfaceOrientations//支持哪些方向{
      return UIInterfaceOrientationMaskPortrait;
    }
    
  • 级别最低的是viewcontroller里的设置

如何实现某一个页面屏幕旋转,而其他页面不旋转

首先, - (BOOL)shouldAutorotate 必须在 self.window.rootViewController 中才能有效果,而且每当手机发生旋转时,就会掉用 rootViewController 的 - (BOOL)shouldAutorotate 方法。

首先我们要保证工程设置为未勾选的状态才行。

Paste_Image.png

一般我们的rootViewController 都是UINavigationcontroller ,所有我们在UINavigationcontroller中设置如下方法
#获取栈最顶端的controller对旋转的支持状态即可,
#然后在每一个VC中都要设置 - (BOOL)shouldAutorotate 来确定当前的VC是否支持横竖屏
# 如果支持,还需要设置 - (NSUInteger)supportedInterfaceOrientations//支持哪些方向

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

如果rootViewController 都是 tarBarController ,所有我们在tarBarController中设置如下方法

- (BOOL)shouldAutorotate{

   return  self.selectedViewController.shouldAutorotate;

}

这样的情况下,每个VC都是默认支持旋转的,那么我们需要在每个VC中都设置- (BOOL)shouldAutorotate吗?答案是否定的。我们可以创建一个controller的基类BaseViewController每个controller都继承BaseViewController ,在BaseViewController中重写- (BOOL)shouldAutorotate 方法中 return NO; 默认关闭,,在需要开启的子类中再次重写- (BOOL)shouldAutorotate 方法,在方法中return YES即可。

下面是一个支持旋转屏幕VC的代码
- (BOOL)shouldAutorotate
 {
      return  YES;
 }
 # 点击全屏
 - (IBAction)large:(id)sender
{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
 # 全屏返回
- (IBAction)largeBack:(id)sender
{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
# 在这个方法中设置屏幕旋转时的 页面设置。
- (void)willRotateToInterfaceOrientation:    (UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
    self.navigationController.navigationBarHidden = NO;
    self.toolBar.hidden = NO;
    self.largeBackButton.hidden = YES;
    self.bottomView.hidden = NO;
    self.largeTitleLabel.hidden = YES;
    self.localRecrodContraint.constant = 10;
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
       toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.navigationController.navigationBarHidden = YES;
        self.localRecrodContraint.constant = 50;
        self.toolBar.hidden = YES;
        self.largeTitleLabel.hidden = NO;
        self.largeBackButton.hidden = NO;
        self.bottomView.hidden = YES;
    }
}

******************更新****************

上面的方法完美解决了我一个工程的单页面屏幕旋转问题,可是在另一个工程中,上面的方法确出现了一个Bug,真的很蛋疼。

按照上面的方法我确实达到了,单页面旋转,其他页面不旋转的效果,但是有个问题: 在页面不旋转的情况下,状态栏确会随着手机的旋转而旋转,着实蛋疼。目前不清楚为什么一样的设置,在两个项目中效果不一样。

解决方法:

 # AppDelegate 中
@property (strong, nonatomic) MyNavigationController* nav;  
 //手机方向发生变化时就会掉用
   手机方向不发生变化时就不会掉用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return self.nav.topViewController.supportedInterfaceOrientations;//支持所有方向
}
 # MyNavigationController中
- (BOOL)shouldAutorotate
{
    return  self.topViewController.shouldAutorotate;   
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}
 # BaseViewController中
- (BOOL)shouldAutorotate
{
    return NO;
}
 //不设置的话,虽然页面是没有旋转,但是状态栏会随着手机旋转而旋转。
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

 #需要旋转的VC中
- (BOOL)shouldAutorotate
{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

可见,状态栏的方向跟页面支持的方向是一样的。所以页面支持的方向是一定要设定的,不设定的话就是默认的:左中右。

屏幕旋转中的其它问题
  • 如何应用程序刚启动时判断设备方向呢?之前说的那些都是都是在rootViewController之后去判断的,但是,在程序刚刚启动时做这些判断都是无效的。下面是网上的一个方法(未验证),在didFinishLaunchingWithOptions函数中:

    //注册通知
    UIDevice *device = [UIDevice currentDevice];
    [device beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenterdefaultCenter] addObserver: self
             selector: @selector(deviceOrientationDidChangeAction:)
                                               name: UIDeviceOrientationDidChangeNotification
                                             object: nil];  
    
    [device endGeneratingDeviceOrientationNotifications];
     //转屏处理函数:
    
    - (void) deviceOrientationDidChangeAction:(NSNotification *)note
    
    {
    
       NSInteger currentOrientation = [[note object] orientation];
    
       switch (currentOrientation)  {
              case0: {   //未知方向
                 break;
          }
          case1: {   //home键向下
                break;
          }
          case2: {   //home键向上
                break;
          }
          case3: {  //home键向左
                break;
          }
          case4: {  //home键向右
                 break;
          }
          default:
                break;
      }
    }
    
    还要在恰当的时候移除通知  不然会被反复调用:
    
    [[NSNotificationCenterdefaultCenter]   removeObserver:self  name:UIDeviceOrientationDidChangeNotification   object:nil];
    
  • 屏幕旋转时,状态栏会默认隐藏的,如何显示出来

    //iOS8 横屏的时候系统默认隐藏了
    [UIApplication sharedApplication].statusBarHidden = YES;
    [UIApplication sharedApplication].statusBarHidden = NO;
    
     # 请注意,上面的俩条一条都不可以少,而且也不可以颠倒顺序。我看着也醉了。但是却是有作用。
    

小结

关于屏幕旋转的问题,目前先写这些,后续如果有新的东西收获,会更新上去的。

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

推荐阅读更多精彩内容