iOS锦囊

【iOS】横竖屏的实现 [类似腾讯视频APP]

2020-03-09  本文已影响0人  片片碎

1、需求描述

小编现在收到的需求是:

2、实现探索

//是否自动旋转,返回YES可以自动旋转  
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
//返回支持的方向  
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
//这个是返回优先方向  
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

详细参考这位老铁的文章,很详细: - iOS 获取屏幕方向,和强制屏幕旋转

后来按照一位老铁的方法,在进入后台时,记录方向,进入前台时,在applicationDidBecomeActive时,强制选转方向。这个方法是可以实现这个需求的,但是不是那么优雅,有旋转的动画,看了腾讯视频的APP是没有动画的,所以想着应该还有更完美的方法
详细参考这位老铁的文章,很详细: - iOS的app的屏幕在横屏进入后台再从后台恢复时却变为竖屏

2、终极方案

终极方案,可以实现到腾讯视频那种效果,中心思想就是旋转windows,核心方法如下:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

详细实现如下:

//AppDelegate.h中 声明一个变量 标示需要什么样的方向

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
  DeviceRotation_Portrait,// 竖屏
  DeviceRotation_Landscape,// 横屏
  DeviceRotation_ALl_Rotation,// 所有屏幕屏 (小编目前没用到)
} DeviceRotation;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property(nonatomic,assign)DeviceRotation allowRotation; 

@end
//AppDelegate.h中 声明一个变量 标示需要什么样的方向

#import "AppDelegate.h"

@implementation AppDelegate
#pragma mark --屏幕
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation == DeviceRotation_Landscape) {
        return UIInterfaceOrientationMaskLandscape;
    }
    else if (self.allowRotation == DeviceRotation_ALl_Rotation) {
      return UIInterfaceOrientationMaskAll;
    }
    else {
        return (UIInterfaceOrientationMaskPortrait);
    }
}
@end
VB中的实现
# pragma mark --按钮切换横竖屏 (默认是竖屏)
- (void)screenButtonAction:(UIButton *)sender {
   sender.selected = !sender.selected;
  [self.navigationController setNavigationBarHidden:NO animated:NO];
  AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  app.allowRotation = DeviceRotation_Portrait;
  self.hiddenStatusBar = NO; //注意下 旧版本 横屏不会隐藏状态栏 所以要自己实现一下
  if (sender.selected) {
    orientation = UIInterfaceOrientationLandscapeRight;
    app.allowRotation = DeviceRotation_Landscape;
    self.hiddenStatusBar = YES;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
  }
  SEL selector = NSSelectorFromString(@"setOrientation:");
  NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
  [invocation setSelector:selector];
  [invocation setTarget:[UIDevice currentDevice]];
  int val = orientation;
  [invocation setArgument:&val atIndex:2];//前两个参数已被target和selector占用
  [invocation invoke];
  
}
# pragma mark --状态栏
- (BOOL)prefersStatusBarHidden {
  return self.hiddenStatusBar;
}

上一篇下一篇

猜你喜欢

热点阅读